Skip to content

Instantly share code, notes, and snippets.

@smitroshin
Created July 6, 2020 16:05
Show Gist options
  • Save smitroshin/80886ff60f1c5c7dca78d18b145afd8f to your computer and use it in GitHub Desktop.
Save smitroshin/80886ff60f1c5c7dca78d18b145afd8f to your computer and use it in GitHub Desktop.
Reading file contents from file input via FileReader
/**
* Source: https://react-dropzone.js.org/
*/
const onDrop = () => (acceptedFiles) => {
acceptedFiles.forEach((file) => {
const reader = new FileReader()
reader.onabort = () => console.log('file reading was aborted')
reader.onerror = () => console.log('file reading has failed')
reader.onload = () => {
// Do whatever you want with the file contents
const binaryStr = reader.result
console.log(binaryStr)
}
reader.readAsArrayBuffer(file)
})
}
// Call onDrop on input change event
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment