Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The problem is that <code>BlobBuilder.append(xhr.responseText)</code> is detecting its argument as a UTF-8 string, which is what XHR returns, and not binary data, which is what it really is. There's a couple of tricks to get the BlobBuilder reading it as binary data instead of string data:</p> <pre><code>var xhr = new XMLHttpRequest(); var photoOrigUrl = 'http://www.google.ru/images/nav_logo72.png'; xhr.open('GET', photoOrigUrl, true); // CHANGE 1: This stops the browser from parsing the data as UTF-8: xhr.overrideMimeType('text/plain; charset=x-user-defined'); xhr.onreadystatechange = function() { if (xhr.readyState == 4 &amp;&amp; xhr.status == 200) { var contentType = xhr.getResponseHeader('Content-type'); fsLink.root.getFile('nav_logo72.png', {'create': true}, function(fileEntry) { fileEntry.createWriter(function(fileWriter) { // CHANGE 2: convert string object into a binary object var byteArray = new Uint8Array(xhr.response.length); for (var i = 0; i &lt; xhr.response.length; i++) { byteArray[i] = xhr.response.charCodeAt(i) &amp; 0xff; } var BlobBuilderObj = new (window.BlobBuilder || window.WebKitBlobBuilder)(); // CHANGE 3: Pass the BlobBuilder an ArrayBuffer instead of a string BlobBuilderObj.append(byteArray.buffer); // CHANGE 4: not sure if it's needed, but keep only the necessary // part of the Internet Media Type string fileWriter.write(BlobBuilderObj.getBlob(contentType.split(";")[0])); }, function(resultError) { console.log('writing file to file system failed ( code ' + resultError.code + ')'); }); }); } } xhr.send(); </code></pre> <p>This gave me a file with the same length as what <code>xhr.getResponseHeader('Content-Length')</code> suggests it should have been.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload