javascript - Why length of XMLHttpRequest response differs from size of requested file? - Stack Overflow

If actual size of file is 6327 bytes (and content-length in response header is 6327),then why length of

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).

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745181026a4615418.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信