Tuesday, 15 January 2013

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


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

So why is the XMLHttpRequest response length different (the value of xhr.response.length in the code below):

6085 in Chrome (version 41) and 5961 in Firefox (version 36)?

// This code to avoid access error somewhere on google.com var xhr = new XMLHttpRequest (); Xhr.open ('receive', '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', 'app / zip');  

does not help.

Mime type should be override:

  var xhr = new XMLHttpRequest (); Xhr.open ('receive', '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, this is not the best answer to the question, but works correctly (xhr.response.length is equal to the file size). 


No comments:

Post a Comment