我正在创建一个简单的表单来使用具有 redux 表单和材料 ui 的电子react样板上传文件。
问题是我不知道如何创建输入文件字段,因为材料 ui 不支持上传文件输入。
关于如何实现这一目标的任何想法?
我正在创建一个简单的表单来使用具有 redux 表单和材料 ui 的电子react样板上传文件。
问题是我不知道如何创建输入文件字段,因为材料 ui 不支持上传文件输入。
关于如何实现这一目标的任何想法?
APIcomponent为此目的而提供。
<Button
  variant="contained"
  component="label"
>
  Upload File
  <input
    type="file"
    hidden
  />
</Button>
较新的 MUI 版本:
<input
  accept="image/*"
  className={classes.input}
  style={{ display: 'none' }}
  id="raised-button-file"
  multiple
  type="file"
/>
<label htmlFor="raised-button-file">
  <Button variant="raised" component="span" className={classes.button}>
    Upload
  </Button>
</label> 
你需要用 组件,并添加值为 'label' 的containerElement属性...
<RaisedButton
   containerElement='label' // <-- Just add me!
   label='My Label'>
   <input type="file" />
</RaisedButton>
您可以在此 GitHub问题 中阅读有关它的更多信息。
编辑:更新 2019。
检查@galki的底部答案
TLDR;
<input
  accept="image/*"
  className={classes.input}
  style={{ display: 'none' }}
  id="raised-button-file"
  multiple
  type="file"
/>
<label htmlFor="raised-button-file">
  <Button variant="raised" component="span" className={classes.button}>
    Upload
  </Button>
</label> 
这是一个使用 IconButton 使用 v3.9.2 捕获输入(照片/视频捕获)的示例:
import React, { Component, Fragment } from 'react';
import PropTypes from 'prop-types';
import { withStyles } from '@material-ui/core/styles';
import IconButton from '@material-ui/core/IconButton';
import PhotoCamera from '@material-ui/icons/PhotoCamera';
import Videocam from '@material-ui/icons/Videocam';
const styles = (theme) => ({
    input: {
        display: 'none'
    }
});
class MediaCapture extends Component {
    static propTypes = {
        classes: PropTypes.object.isRequired
    };
    state: {
        images: [],
        videos: []
    };
    handleCapture = ({ target }) => {
        const fileReader = new FileReader();
        const name = target.accept.includes('image') ? 'images' : 'videos';
        fileReader.readAsDataURL(target.files[0]);
        fileReader.onload = (e) => {
            this.setState((prevState) => ({
                [name]: [...prevState[name], e.target.result]
            }));
        };
    };
    render() {
        const { classes } = this.props;
        return (
            <Fragment>
                <input
                    accept="image/*"
                    className={classes.input}
                    id="icon-button-photo"
                    onChange={this.handleCapture}
                    type="file"
                />
                <label htmlFor="icon-button-photo">
                    <IconButton color="primary" component="span">
                        <PhotoCamera />
                    </IconButton>
                </label>
                <input
                    accept="video/*"
                    capture="camcorder"
                    className={classes.input}
                    id="icon-button-video"
                    onChange={this.handleCapture}
                    type="file"
                />
                <label htmlFor="icon-button-video">
                    <IconButton color="primary" component="span">
                        <Videocam />
                    </IconButton>
                </label>
            </Fragment>
        );
    }
}
export default withStyles(styles, { withTheme: true })(MediaCapture);
这对我有用(“@material-ui/core”:“^4.3.1”):
    <Fragment>
        <input
          color="primary"
          accept="image/*"
          type="file"
          onChange={onChange}
          id="icon-button-file"
          style={{ display: 'none', }}
        />
        <label htmlFor="icon-button-file">
          <Button
            variant="contained"
            component="span"
            className={classes.button}
            size="large"
            color="primary"
          >
            <ImageIcon className={classes.extendedIcon} />
          </Button>
        </label>
      </Fragment>