【发布时间】:2020-10-06 18:58:07
【问题描述】:
我正在尝试使用 React.js 读取多个文件,但我的代码只读取一个文件,而没有读取其余文件。有什么建议吗?
谢谢
constructor(props) {
super(props);
this.state = {
files: [],
changedFileIndex: -1,
fileReader : null
};
this.fileUploaderRef = React.createRef();
}
handleFileReader = (e)=>{
console.log("handleFileReader")
var content =this.state.fileReader.result;
console.log(content);
}
handleFileChosen(file){
console.log("handleFileChosen")
console.log(file.result)
this.state.fileReader=new FileReader();
this.state.fileReader.onloadend = this.handleFileReader;
this.state.fileReader.readAsText(file);
}
async readAllFiles (AllFiles) {
console.log("readAllFiles")
//console.log(AllFiles[0].name)
AllFiles.map((file)=>
{
this.handleFileChosen(file)
}
);
}
在文件数组中,我们需要遍历文件并发送给其他函数,以便将每个文件的内容写入数组中。 经过一些调试,例如对于 2 个文件,看起来代码执行 'handleFileChosen' 2 次,然后转到 handleFileReader 2 次,这可能是错误的,但我不知道如何解决这个问题。相反,它应该是这样的:执行 'HandleFileReader',然后执行 'handleFileChosen',然后再执行 'HandleFileReader',然后执行 'handleFileChosen'
【问题讨论】:
标签: javascript reactjs file