Last active
December 21, 2015 20:09
-
-
Save rclark/6359602 to your computer and use it in GitHub Desktop.
How to drag-drop files into the browser
This file contains 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
<!doctype html> | |
<html> | |
<head> | |
<meta charset="utf8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1"> | |
</head> | |
<style type="text/css"> | |
* { | |
margin: 0; | |
padding: 0; | |
} | |
html, body, #drop { | |
height: 100%; | |
width: 100%; | |
} | |
#drop { | |
text-align: center; | |
border-width: 15px; | |
border-style: solid; | |
box-sizing: border-box; | |
display: table; | |
} | |
#drop > h1 { | |
display: table-cell; | |
vertical-align: middle; | |
} | |
</style> | |
<body> | |
<div id="drop" style="border-color:#add8e6;"> | |
<h1>Please drop a file here</h1> | |
</div> | |
</body> | |
<script src="index.js"></script> | |
</html> |
This file contains 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
if (window.FileReader) { | |
function fileDragHover(e) { | |
e.stopPropagation(); | |
e.preventDefault(); | |
dropzone.style.borderColor = "#20be20"; | |
return false; | |
} | |
function fileDragLeave(e) { | |
e.stopPropagation(); | |
e.preventDefault(); | |
dropzone.style.borderColor = "#add8e6"; | |
return false; | |
} | |
function fileDrop(e) { | |
e.stopPropagation(); | |
e.preventDefault(); | |
var files = e.target.files || e.dataTransfer.files, | |
reader = new FileReader(); | |
reader.onload = function (e) { | |
console.log(e.target.result); | |
}; | |
reader.onerror = function (e) { | |
console.log(e.target.error.code); | |
}; | |
reader.readAsText(files[0]); | |
} | |
var dropzone = document.getElementById('drop'); | |
dropzone.addEventListener('dragover', fileDragHover, false); | |
dropzone.addEventListener('dragleave', fileDragLeave, false); | |
dropzone.addEventListener('drop', fileDrop, false); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment