Skip to content

Instantly share code, notes, and snippets.

@ajace
Created August 26, 2013 14:08
Show Gist options
  • Save ajace/6341793 to your computer and use it in GitHub Desktop.
Save ajace/6341793 to your computer and use it in GitHub Desktop.
Javascript drag and drop with progress notification
function(file, index){
var url = module.attr('data-upload');
var xhr = new XMLHttpRequest(),
formData = new FormData(),
previewImage,
img;
xhr.upload.addEventListener("progress", function (event) {
$progress.stop().css('width', 0);
if (event.lengthComputable) {
var percentage = Math.round( 100 * ( event.loaded / event.total ) );
$progress.stop().animate({ width: percentage + '%' }, 50);
}
}, false);
xhr.addEventListener("load", function(event){
multipleFiles();
$progress.stop().css('width', '100%');
$complete.stop().css('display','inline-block');
}, false);
xhr.addEventListener('loadend', function(event){
currentCount++;
if ( currentCount===numOfFiles ) {
multipleFiles();
setTimeout(function(){
$dialogue.fadeOut('slow');
$cheech.fadeOut('slow');
}, 700);
}
$.parseJSON(xhr.responseText);
}, false);
xhr.open('POST', url, true);
xhr.send(formData);
// create a new FileReader for faster previews, separate from main upload
var preview = new FileReader();
preview.addEventListener('load', function(){
previewImage = new Image();
img = document.createElement('img');
img.className = 'land';
img.file = file;
img.src = preview.result;
}, false);
preview.readAsDataURL(file);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment