Khalid Manasra
Khalid Manasra

Reputation: 23

XMLHttpRequest content-length is not equal to the file actual size

i am trying to use XMLHttpRequest to upload file(image) to my server side, well, i check the file size before sending and check the content-length on my request header, they are not the same, here is the code :

var file = 'someFile'; 
var readers = new FileReader();
readers.readAsBinaryString(file);
readers.onload = (function(theFile) {
  return function(e) {                     
       var myData =  e.target.result;
       console.log(myData.length); // this output is 5548
       var xhr = new XMLHttpRequest();
       xhr.open("POST", url, true);
       xhr.setRequestHeader('Content-Type', 'application/octet-stream;');
       // send the collected data as JSON
       xhr.send(myData );
       xhr.onloadend = function () {
          // done               
       };
  })(file);

when i am looking at the content-lenght on my header request i found the length is bigger than the actaul size , some number like ~7500,

how i can make sure the request file data is the same as the actual size of the file?

Upvotes: 0

Views: 4410

Answers (1)

Dawid C
Dawid C

Reputation: 413

When you are sending file content it is usually send as base64-encoded string. This increase size of the request in about 30%. Therefore

console.log(myData.length); // this output is 5548

Is not the size of the file it its length. You could use File.name

Upvotes: 1

Related Questions