I'm using the following code to retrieve data from a dropped/selected file.
onDrop = (files) => {
files.forEach(file => {
const reader = new FileReader();
reader.onload = () => {
const fileAsBinaryString = reader.result
console.log(fileAsBinaryString);
}
reader.onabort = () => console.log('file reading was aborted');
reader.onerror = () => console.log('file reading has failed');
try {
reader.readAsDataURL(file);
} catch(err) {
console.log(err)
console.log(file);
}
this.setState({
fileName: file.name
})
});
}
render() {
return (
<div className="app">
<ReactDropzone onDrop={this.onDrop} className="dropzone">
<IconContext.Provider value={{ size: "5em" }}>
<IoMdCloudUpload/>
</IconContext.Provider>
<h1>{this.state.fileName}</h1>
</ReactDropzone>
</div>
);
}
When I run the server and drop something inside the dropzone, even though console.log(file) gives me a non-blank File object, I get
Error: cannot read as File: {}
At
reader.readAsDataURL(file);
Any idea as to why this may happen and how I should fix it?