Skip to content

Instantly share code, notes, and snippets.

@sebastiangeiger
Created May 13, 2012 13:36
Show Gist options
  • Select an option

  • Save sebastiangeiger/2688497 to your computer and use it in GitHub Desktop.

Select an option

Save sebastiangeiger/2688497 to your computer and use it in GitHub Desktop.
FileReader API example
<html>
<title>File API</title>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
console.log("loaded");
var holder = document.getElementById('holder'),
state = document.getElementById('status');
console.log(holder);
if (typeof window.FileReader === 'undefined') {
state.className = 'fail';
} else {
state.className = 'success';
state.innerHTML = 'File API & FileReader available';
}
holder.ondragover = function () { this.className = 'hover'; return false; };
holder.ondragend = function () { this.className = ''; return false; };
holder.ondrop = function (e) {
this.className = '';
e.preventDefault();
var file = e.dataTransfer.files[0],
reader = new FileReader();
reader.onload = function (event) {
console.log("Onload event fired");
console.log(event.target);
holder.style.background = 'url(' + event.target.result + ') no-repeat center';
};
console.log(file);
reader.readAsDataURL(file);
return false;
};
});
</script>
<style>
#holder { border: 10px dashed #ccc; width: 300px; height: 300px; margin: 20px auto;}
#holder.hover { border: 10px dashed #333; }
</style>
</head>
<body>
<div id="holder">Drop your files here!</div>
<div id="status">Status: </div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment