Reputation: 354
If actual size of file is 6327 bytes (and content-length in response header is 6327),
then why length of XMLHttpRequest response (value of xhr.response.length in code below) is different:
6085 in Chrome (Version 41) and 5961 in Firefox (Version 36)?
//run this code somewhere on google.com to avoid access error
var xhr = new XMLHttpRequest();
xhr.open('GET','https://www.google.com/images/errors/robot.png',true);
xhr.onreadystatechange=function(e){
if(xhr.readyState==4&&xhr.status==200){
console.log(xhr.response.length);
}
};
xhr.send();
Command
xhr.setRequestHeader('Content-Type','image/png');
or command
xhr.setRequestHeader('Content-Type','application/zip');
does not help.
Upvotes: 4
Views: 7414
Reputation: 354
MIME type must be overridden:
var xhr = new XMLHttpRequest();
xhr.open('GET','https://www.google.com/images/errors/robot.png',true);
xhr.overrideMimeType('text/plain; charset=x-user-defined');
xhr.onreadystatechange=function(e){
if(xhr.readyState==4&&xhr.status==200){
console.log(xhr.response.length);
}
};
xhr.send();
Actually, it not the best answer to question, but works correctly (value of xhr.response.length is equal to file size).
Upvotes: 4