Home How to obtain FileReader result?
Reply: 0

How to obtain FileReader result?

blazerix
1#
blazerix Published in 2017-12-07 15:05:48Z

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?

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.300861 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO