I'm currently working on a file uploader that will eventually be able to loop through multiple files and tally up the total amount of bytes to upload by looping through them all beforehand. However, I've come across the following problem...
If I compare the file size returned by target.files[n].size
from a file input and the value returned by XHR's upload progress listener event e.total
, they don't seem to be the same. XHR's e.total
ends up being several hundred bytes higher sometimes.
Could someone explain why this is happening? This happens regardless of what browser I'm using to test the Javascript.
<form enctype="multipart/form-data" method="post">
<input type="file" multiple="" />
</form>
function uploadfiles() {
var files = $('input[type=file]').prop('files');
var total_size = files[0].size;
console.log("size from file.size = " + total_size);
var fd = new FormData();
fd.append('file', files[0]);
$.ajax({
url: '/ajax/uploadfiles.php',
type: 'post',
processData: false,
contentType: false,
data: fd,
dataType: 'json',
xhr: function() {
var xhr = new XMLHttpRequest();
xhr.upload.addEventListener('progress', function(e){
console.log("xhr size = " + e.total);
}, false);
return xhr;
}
}); // ajax
};