I used this snippet on several of my sites, it handles loading, dragging and dropping Multiples files and a progress bar.
HTML
, #output .
JS
dataTransfer jQuery drop.
$(document).ready(function(){
$.event.props.push("dataTransfer");
$('#output').bind({
"dragenter dragexit dragover" : do_nothing,
drop : drop
});
});
function do_nothing(evt){
evt.stopPropagation();
evt.preventDefault();
}
function update_progress(evt,fic) {
var id_tmp=fic.size;
if (evt.lengthComputable) {
var percentLoaded = Math.round((evt.loaded / evt.total) * 100);
if (percentLoaded <= 100) {
$('#'+id_tmp+' .percent').css('width', percentLoaded + '%');
$('#'+id_tmp+' .percent').html(percentLoaded + '%');
}
}
}
, drop
function drop(evt){
do_nothing(evt);
var files = evt.dataTransfer.files;
if(files.length>0){
for(var i in files){
if(files[i].size!=undefined) {
var fic=files[i];
xhr = jQuery.ajaxSettings.xhr();
if(xhr.upload){
xhr.upload.addEventListener('progress', function (e) {
update_progress(e,fic);
},false);
}
provider=function(){ return xhr; };
var fd=new FormData;
fd.append('fic',fic);
$.ajax({
url:'/path/to/save_fic.php',
type: 'POST',
data: fd,
xhr:provider,
processData:false,
contentType:false,
complete:function(data){
$('#'+data.responseText+' .percent').css('width', '100%');
$('#'+data.responseText+' .percent').html('100%');
}
});
var id_tmp=fic.size;
$('#output').after('<div class="progress_bar loading" id="'+id_tmp+'"><div class="percent">0%</div></div>');
$('#output').addClass('output_on');
$('#output-listing').append('<li>'+files[i].name+'</li>');
}
}
}
}
IE9 .
, !
( )
XMLHttpRequest
datatransfer
:
PHP
, $_FILES .....
100% , php script .