Skip to content

Instantly share code, notes, and snippets.

@huozhi
Created January 24, 2018 06:23
Show Gist options
  • Save huozhi/4d2cfb09ad54c3a39d4f941c2947e20a to your computer and use it in GitHub Desktop.
Save huozhi/4d2cfb09ad54c3a39d4f941c2947e20a to your computer and use it in GitHub Desktop.
read content when drop file with drag events
const readOnDrop = (element, onRead) => {
element.addEventListener('dragover', ev => ev.preventDefault())
element.addEventListener('drop', ev => {
ev.preventDefault()
const dt = ev.dataTransfer
const file = dt.files[0]
const reader = new FileReader()
reader.onload = event => onRead(event.target.result)
reader.readAsText(file)
})
}
export default readOnDrop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment