I'm trying to read multiple files with React.js, but my code reads only one file and doesn't read the rest. Any suggestion?
Thanks
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)
}
);
}
In the array of files, we need to loop over the files and send to the other functions in order to write content of each file in the array. After some debugging, for example for 2 files, it looks like the code executes 'handleFileChosen' 2 times, and then goes to handleFileReader 2 times which is probably what's wrong but I'm not sure how to fix this. Instead, it should be like this: execute 'HandleFileReader', then execute 'handleFileChosen', then again 'HandleFileReader', then execute 'handleFileChosen'