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.
I would use Papa Parse (https://www.npmjs.com/package/papaparse). And here is an example react component:
class FileReader extends React.Component {
constructor() {
super();
this.state = {
csvfile: undefined
};
this.updateData = this.updateData.bind(this);
}
handleChange = event => {
this.setState({
csvfile: event.target.files[0]
});
};
importCSV = () => {
const { csvfile } = this.state;
Papa.parse(csvfile, {
complete: this.updateData,
header: true
});
};
updateData(result) {
var data = result.data;
console.log(data);
}
render() {
console.log(this.state.csvfile);
return (
Import CSV File!
{
this.filesInput = input;
}}
name="file"
placeholder={null}
onChange={this.handleChange}
/>
);
}
}
export default FileReader;