Home  >  Q&A  >  body text

javascript - react input file

1 There is a hidden input type file element in my page. This element can be obtained through the ref attribute and is named this.inputFile
2 There is a button on the right side of the page header. Click the button , trigger this, fileInput.click()
3 but the result is that the file selection box cannot pop up. Really, what is the problem? The logic is okay and embarrassing

class FileManage extends Component{

constructor(props){
    super(props);
    this.onHandleBack = this.onHandleBack.bind(this);
    this.showOperationSheet = this.showOperationSheet.bind(this);
}   
onHandleBack(){
    this.props.history.goBack();
}

showOperationSheet(){
    console.log('点击上传');
    if(this.fileInput){
        console.log('进入判断');
        //很神奇,必须有这行代码,才能调用图片选择,我也很无奈啊。
        console.log(this.fileInput.click());
        this.fileInput.click()
    }
};
render(){
         
    return (
        <p>
            <p onClick={this.showOperationSheet}>按钮</p>
            <p style={{display:"none"}}>
                <form action="" encType="multipart/form-data" method='POST' onSubmit={this._onSubmit}>
                    <input type="file" ref={(input)=>{this.fileInput = input}} onChange={this._onChange}/>
                </form>
            
            </p>
        </p>
        
    )
} 

}
export default FileManage

我想大声告诉你我想大声告诉你2634 days ago917

reply all(1)I'll reply

  • 欧阳克

    欧阳克2017-07-05 10:46:09

    Post the specific code, it’s so hard to figure it out

    reply
    0
  • Cancelreply