如何使用自定义按钮在 reactjs 中读取和上传文件

Blu*_*old 11 javascript reactjs

我想在本地上传和读取文件,但我想使用不使用 HTML 输入的自定义按钮来做到这一点。

<input type="file" id="my_file_input" />
Run Code Online (Sandbox Code Playgroud)

我找到了这种方式,但我不想使用此形状或此按钮,我想使用材质 UI Raised Button 来执行此功能以匹配其他站点的 Button。

我也尝试了以下方法,但没有用,因为当我点击按钮时什么也没发生。

<input type="file" id="my_file_input" style={{display:"none"}}/>
<label htmlFor="my_file_input">
    <RaisedButton
        label="Import from Excel"
        labelColor="#FFFFFF"
        backgroundColor="#01579b"
        />
</label>
Run Code Online (Sandbox Code Playgroud)

我以为我应该在 的onClick功能中手动执行上传/读取文件功能,RaisedButton但我没有找到方法来做到这一点。

那么在反应中有没有其他解决方案可以解决这个问题?

Ken*_*Ken 14

我想提供有关将 refs 与功能组件一起使用的更新。这是一个简单的例子:

Import React, {useRef} from 'React'

const myComponent = () => {
  const fileInputRef=useRef();
  
  const handleChange(event) = () =>{
    // do something with event data

  }
  
  return(
    <>
      <button onClick={()=>fileInputRef.current.click()}>
        Custom File Input Button
      </button>
      <input onChange={handleChange} multiple={false} ref={fileInputRef} type='file'hidden/>
    </>
  )
}
Run Code Online (Sandbox Code Playgroud)


Pen*_*ava 9

我希望这段代码能帮到你。我们可以通过两种方式解决。

1-)

HTML

<div>
<input type="file" hidden ref={this.inputReference} onChange={this.fileUploadInputChange} />
<button className="ui button" onClick={this.fileUploadAction}>
    Image Upload
</button>
{this.state.fileUploadState}
</div>
Run Code Online (Sandbox Code Playgroud)

反应构造函数

constructor(props) {
    super(props);
    this.state={fileUploadState:""}
    this.inputReference = React.createRef();
}
Run Code Online (Sandbox Code Playgroud)

点击功能

fileUploadAction = () =>this.inputReference.current.click();
fileUploadInputChange = (e) =>this.setState({fileUploadState:e.target.value});
Run Code Online (Sandbox Code Playgroud)

2-)

HTML

<div>
<input id="fileButton" type="file" hidden />
<button onClick={this.fileUploadButton}>
    Image Upload
</button>
{this.state.fileUploadState}
</div>
Run Code Online (Sandbox Code Playgroud)

反应状态

this.state = {fileUploadState:""}
Run Code Online (Sandbox Code Playgroud)

反应函数

fileUploadButton = () => {
document.getElementById('fileButton').click();
document.getElementById('fileButton').onchange = () =>{      
this.setState({
    fileUploadState:document.getElementById('fileButton').value
        });
    }
}
Run Code Online (Sandbox Code Playgroud)


shu*_*man 2

请阅读React Material的API https://material-ui.com/demos/buttons/

import React from 'react';
import PropTypes from 'prop-types';
import { withStyles } from '@material-ui/core/styles';
import Button from '@material-ui/core/Button';

const styles = theme => ({
  button: {
    margin: theme.spacing.unit,
  },
  input: {
    display: 'none',
  },
});

function ContainedButtons(props) {
  const { classes } = props;
  return (
    <div>
      <input
        accept="image/*"
        className={classes.input}
        id="contained-button-file"
        multiple
        type="file"
      />
      <label htmlFor="contained-button-file">
        <Button variant="raised" component="span" className={classes.button}>
          Upload
        </Button>
      </label>
    </div>
  );
}

ContainedButtons.propTypes = {
  classes: PropTypes.object.isRequired,
};

export default withStyles(styles)(ContainedButtons);
Run Code Online (Sandbox Code Playgroud)