Ajax progress bar on upload
Hi,
I'm having difficulty in getting the progress of the file being uploaded to be shown. I've searched all over and could swear I'm doing everything right but the only event that seems to be fired is the event.loaded, abort and error. I've checked moziila's page on this and stackoverflow and to no avail. Can anyone let me know where I could be going wrong? thanks
-
$("#uploadSubmitBtn").click(function(){
var formData = new FormData($("#mediaUploadForm")[0]);
var usersFiles=$("#uploadBtn")[0].files;
formData.append('usersUpload', usersFiles);
$.ajax({
//for the progress bar...
xhr: function() {
var xhr = new XMLHttpRequest();
xhr.addEventListener("load", function(evt){
//seems like 'load' fired when client done sending to the server...
//ergo DONE uploading but how do we get it while in process of sending...
if(evt.lengthComputable){
var percentComplete = Math.floor( (evt.loaded / evt.total) * 100);
$("#uploadPercent").text(percentComplete + "%::loaded");
}else{
alert.log("unable to compute file size");
}
}, false);
xhr.addEventListener("error", function(err){
alert("uploading stopped with an error");
},false);
xhr.addEventListener("abort", function(){
alert("upload aborted");
}, false);
if(xhr.upload) {
//xhr.upload.addEventListener("progress", calPercentage, false);
//xhr.upload.onprogress = calPercentage();
xhr.upload.addEventListener("progress", function(evt){
if(evt.lengthComputable){
//if no lengthComputable use the size from the fileSize, that == the total
var percentComplete = Math.floor( (evt.loaded / evt.total) * 100);
$("#uploadPercent").text(percentComplete + "%::progress...");
}else{
alert("unable to compute file size");
}
},false);
}
return xhr;
},
//other stuff
url : "upload",
type : 'post',
data : formData,
async : false,
cache: false,
processData: false,
contentType: false,
success : function(resp){
$('#msgBoxText').text(resp);
$('#msgBox').slideDown("slow");
},
});
});//end of submitting media