Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 将文件设置为状态_Reactjs - Fatal编程技术网

Reactjs 将文件设置为状态

Reactjs 将文件设置为状态,reactjs,Reactjs,在我的react组件中,我有: state = { title: '', content: '', file: '' } handleChange = (e) => { this.setState({ [e.target.id] : e.target.value }) } handleSubmit = (e) => { e.preve

在我的react组件中,我有:

 state = {
        title: '',
        content: '',
        file: ''
    }
    handleChange = (e) => {
        this.setState({
            [e.target.id] : e.target.value
        })
    }
    handleSubmit = (e) => {
       e.preventDefault();
       console.log(this.state)
    }



 <form onSubmit={this.handleSubmit}>

 <input type="text" id="title" onChange={this.handleChange}/>

 <textarea id="content" onChange={this.handleChange}></textarea>

 <input type="file" id="file" onChange={this.handleChange}/>

</form>
我将进入console.logthis.state文件的唯一路径,如:C:\fakepath\title.jpg。 如何设置为状态实际文件?
提前谢谢你的帮助

对于从事件读取文件,必须使用e.target.files[0] 下面的代码对您来说很好

状态={ 标题:, 内容:, 文件: } handleChange=e=>{ 这是我的国家{ [e.target.id]:e.target.value } } handleOnFileChange=e=>{ 让file=e.target.files[0]; 这是我的国家{ [e.target.id]:文件 } } handleSubmit=e=>{ e、 防止违约; console.logthis.state }
你的意思是显示所选文件吗?现在我如何将此存储文件重新分配给输入文件标记,因为当我更改状态时,文件会自动取消选择?不会,我们不会按状态控制文件输入