I'm using XMLHttpRequest (with jQuery) to get the upload progress of multiple files. By adding a "progress" event listener to the XMLHttpRequest object I can get event.loaded
and event.total
. Those variables give me the loaded and total bytes of all the files combined.
What I'd like to do is get the progress of each individual file, but from what I can see that information isn't available with XMLHttpRequest. Is that true?
I don't think this is even necessary, but here's my code:
var xhr = $.ajaxSettings.xhr();
if (xhr.upload) {
xhr.upload.addEventListener('progress', function(event) {
var percent = 0;
var position = event.loaded || event.position;
var total = event.total;
if (event.lengthComputable) {
percent = Math.ceil(position / total * 100);
}
updateProgressBar(percent);
}, false);
}
return xhr;
If I can accomplish this with XMLHttpRequest that would be great. Any info on this would be appreciated.
This is what i use,
$.ajax({
xhr: function () {
myXhr = $.ajaxSettings.xhr();
myXhr.addEventListener('progress', function (e) { }, false);
if (myXhr.upload) {
myXhr.upload.onprogress = function (e) {
var completed = 0;
if (e.lengthComputable) {
var done = e.position || e.loaded,
total = e.totalSize || e.total;
completed = Math.round((done / total * 1000) / 10);
}
console.log(completed); // Displays the completed percentage
}
}
return myXhr;
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With