I would like the user to upload a .csv file, and then have the browser be able to parse the data from that file. I am using ReactJS. How would this work? Thanks.
Advertisement
Answer
Figured it out. A combination of react-file-reader and HTML5’s FileReader (see this page).
Placed the react-file-reader bit inside of render:
JavaScript
x
4
1
<ReactFileReader handleFiles={this.handleFiles} fileTypes={'.csv'}>
2
<button className='btn'>Upload</button>
3
</ReactFileReader>
4
And then this above.
JavaScript
1
9
1
handleFiles = files => {
2
var reader = new FileReader();
3
reader.onload = function(e) {
4
// Use reader.result
5
alert(reader.result)
6
}
7
reader.readAsText(files[0]);
8
}
9