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 badges1 Answer
Reset to default 4MIME 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
版权声明:本文标题:javascript - Why length of XMLHttpRequest response differs from size of requested file? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745181026a2646465.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论