Skip to content

Instantly share code, notes, and snippets.

@yiwenl
Last active August 2, 2019 09:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yiwenl/d5a4ce5c4507d88b0732f7c49cf9a702 to your computer and use it in GitHub Desktop.
Save yiwenl/d5a4ce5c4507d88b0732f7c49cf9a702 to your computer and use it in GitHub Desktop.
const dropArea = window;
const preventDefaults = (e)=> {
e.preventDefault()
e.stopPropagation();
}
;['dragenter', 'dragover', 'dragleave', 'drop'].forEach(eventName => {
dropArea.addEventListener(eventName, preventDefaults, false);
})
const handleDrop = (e) => {
let dt = e.dataTransfer
let files = dt.files;
const file = files[0]
let reader = new FileReader()
reader.readAsDataURL(file)
reader.onloadend = () => {
let img = document.createElement('img')
img.onload = () => {
console.log('Image loaded');
}
img.src = reader.result;
}
}
dropArea.addEventListener('drop', handleDrop, false);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment