admin管理员组

文章数量:1415653

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 to avoid access error
var xhr = new XMLHttpRequest();
xhr.open('GET','.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 mand

xhr.setRequestHeader('Content-Type','application/zip');

does not help.

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. to avoid access error
var xhr = new XMLHttpRequest();
xhr.open('GET','https://www.google./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 mand

xhr.setRequestHeader('Content-Type','application/zip');

does not help.

Share Improve this question edited Mar 20, 2015 at 7:19 valeryan asked Mar 17, 2015 at 14:11 valeryanvaleryan 3641 gold badge5 silver badges15 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

MIME type must be overridden:

var xhr = new XMLHttpRequest();
xhr.open('GET','https://www.google./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).

本文标签: javascriptWhy length of XMLHttpRequest response differs from size of requested fileStack Overflow