Last active
August 2, 2019 09:48
-
-
Save yiwenl/d5a4ce5c4507d88b0732f7c49cf9a702 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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