reactjs - 文件上传不适用于按钮单击 Material -UI V1 ReactJs

文件上传不适用于 Material-UI/按钮点击。

 <Button dense
      containerElement="label"
      label="label">
         <input
            style={{display: 'none'}}
            type="file" />
  </Button>

最佳答案

我找到了解决方案,但仍需要更好的方法来实现它。

  import React, { Component } from 'react';
  import Button from 'material-ui/Button';
  import FormControl from '../formcontrol';

 class FileUpload extends Component {

constructor(props) {
    super(props);
    this.state = {
        file: []
    };
    this.onClick = this.onClick.bind(this);
    this.onChange = this.onChange.bind(this);
}

onClick = (inputValue) => {
    document.getElementById("fileInput").click()
}

onChange = (file, onChange) => {
    console.log(this.state.file);
}

render() {
    const { classes, onChange } = this.props;
    const { file } = this.state;

    return (
        <FormControl>
            <div>
                <input type="file" style={{ display: 'none' }} id="fileInput" onChange={e => {
                    this.onChange(e.currentTarget.files[0], onChange);
                }} />

                <Button className={classes.button}
                    raised
                    containerElement='label'
                    color="default"
                    label='My Label'
                    onClick={(event) => this.onClick(event)} }>
                    Upload
                < Icon className={this.props.classes.rightIcon}>file_upload</Icon>
                </Button>
            </div>
        </FormControl >
    );

  }

}

export default FileUpload;

https://stackoverflow.com/questions/47523560/

相关文章:

java - 没有数据时应该返回哪个http代码

loops - 循环 Fortran 中的字符

prolog - swi-prolog 中的不相等操作

php - Laravel paginate 无法在 querybuilder 上选择某些列

listview - Xamarin.Forms:软键盘与页面底部的最后一个条目(滚动时)重叠

bash - 在 bash 中有条件地将命令的输出定向到/dev/null

html - 在 HTML 页面上组合 SVG 标签

amazon-web-services - AWS GLUE 数据导入问题

php - php swagger注释jsonproperty中方括号的转义字符

macos - Launchd 无法访问 Mac OS 钥匙串(keychain)