I am trying to read a file from my local machine. I have attached the onFileLoad function to the upload button.
I understand that this is happening asynchronously, but how do I access the contents of the file in another function?
What I mean is, after my file is loaded, I want to be able to store the results (text in the file) into a variable. I'm using React and I want to avoid using global variables.
onFileLoad(event){
var file = event.target.files[0];
if (file){
var reader = new FileReader();
reader.onloadend = function(e) {
window.contents = e.target.result;
}
reader.readAsText(file);
}
else {
alert("Failed to load file");
}
}
What I'd ideally like to do is once the file is loaded, do something like
(this.setState({data:myloadedFile})
Can someone guide me in the right direction?