【发布时间】:2019-02-06 08:08:56
【问题描述】:
我有一个需要放入状态的文件对象。我尝试这样做的原因是它被提供为重新渲染 DOM 以清除 FileReader() 中的值的可能解决方案。这是当前线程源自的链接
Node 'crypto' SHA256 hashing model behaving erratically
我将在此处放置一个精简的 react 组件,以便您查看发生了什么。
import React, { Component } from "react";
class SearchPage extends Component {
constructor(props) {
super(props);
this.state = {
files: []
};
}
onChange(e) {
let files = e.target.files;
this.setState({ files: files[0] });
console.log(files[0]);
console.log(this.state.files)
}
render() {
return (
<div onSubmit={this.onFormSubmit}>
<input type="file" name="file" onChange={e => this.onChange(e)} />
</div>
);
}
}
export default SearchPage;
console.log(files[0]} 给出如下内容:
File(1) {name: "1.txt", lastModified: 1549429792266, lastModifiedDate: Tue Feb 05 2019 22:09:52 GMT-0700 (Mountain Standard Time), webkitRelativePath: "", size: 1, …}
lastModified: 1549429792266
lastModifiedDate: Tue Feb 05 2019 22:09:52 GMT-0700 (Mountain Standard Time) {}
name: "1.txt"
size: 1
type: "text/plain"
webkitRelativePath: ""
如果我console.log(this.state.files) 我得到[]。
我已将初始状态设置为"" 和null 无济于事。我最终想将状态用于FileReader() 函数。
感谢任何想法或解决方案。谢谢!
【问题讨论】: