Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>So I'm answering to myself — and sorry for that — but I think it might be useful for someone as lost as I was ;)</p> <p>So you have to use <a href="https://developer.mozilla.org/en/JavaScript_typed_arrays/ArrayBuffer" rel="noreferrer">ArrayBuffer</a> and set the <code>responseType</code> property of your <code>XMLHttpRequest</code> object instance to <code>arraybuffer</code> for retrieving a native array of Bytes, which can be converted to base64 using the following convenient function (found <a href="https://gist.github.com/958841" rel="noreferrer">there</a>, author may be blessed here):</p> <pre><code>function base64ArrayBuffer(arrayBuffer) { var base64 = '' var encodings = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/' var bytes = new Uint8Array(arrayBuffer) var byteLength = bytes.byteLength var byteRemainder = byteLength % 3 var mainLength = byteLength - byteRemainder var a, b, c, d var chunk // Main loop deals with bytes in chunks of 3 for (var i = 0; i &lt; mainLength; i = i + 3) { // Combine the three bytes into a single integer chunk = (bytes[i] &lt;&lt; 16) | (bytes[i + 1] &lt;&lt; 8) | bytes[i + 2] // Use bitmasks to extract 6-bit segments from the triplet a = (chunk &amp; 16515072) &gt;&gt; 18 // 16515072 = (2^6 - 1) &lt;&lt; 18 b = (chunk &amp; 258048) &gt;&gt; 12 // 258048 = (2^6 - 1) &lt;&lt; 12 c = (chunk &amp; 4032) &gt;&gt; 6 // 4032 = (2^6 - 1) &lt;&lt; 6 d = chunk &amp; 63 // 63 = 2^6 - 1 // Convert the raw binary segments to the appropriate ASCII encoding base64 += encodings[a] + encodings[b] + encodings[c] + encodings[d] } // Deal with the remaining bytes and padding if (byteRemainder == 1) { chunk = bytes[mainLength] a = (chunk &amp; 252) &gt;&gt; 2 // 252 = (2^6 - 1) &lt;&lt; 2 // Set the 4 least significant bits to zero b = (chunk &amp; 3) &lt;&lt; 4 // 3 = 2^2 - 1 base64 += encodings[a] + encodings[b] + '==' } else if (byteRemainder == 2) { chunk = (bytes[mainLength] &lt;&lt; 8) | bytes[mainLength + 1] a = (chunk &amp; 64512) &gt;&gt; 10 // 64512 = (2^6 - 1) &lt;&lt; 10 b = (chunk &amp; 1008) &gt;&gt; 4 // 1008 = (2^6 - 1) &lt;&lt; 4 // Set the 2 least significant bits to zero c = (chunk &amp; 15) &lt;&lt; 2 // 15 = 2^4 - 1 base64 += encodings[a] + encodings[b] + encodings[c] + '=' } return base64 } </code></pre> <p>So here's a working code:</p> <pre><code>var xhr = new XMLHttpRequest(); xhr.open('GET', 'http://some.tld/favicon.png', false); xhr.responseType = 'arraybuffer'; xhr.onload = function(e) { console.log(base64ArrayBuffer(e.currentTarget.response)); }; xhr.send(); </code></pre> <p>This will log a <em>valid</em> base64 encoded string representing the binary file contents.</p> <p><em>Edit:</em> For older browsers not having access to <code>ArrayBuffer</code> and having <code>btoa()</code> failing on encoding characters, here's another way to get a base64 encoded version of any binary:</p> <pre><code>function getBinary(file){ var xhr = new XMLHttpRequest(); xhr.open("GET", file, false); xhr.overrideMimeType("text/plain; charset=x-user-defined"); xhr.send(null); return xhr.responseText; } function base64Encode(str) { var CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; var out = "", i = 0, len = str.length, c1, c2, c3; while (i &lt; len) { c1 = str.charCodeAt(i++) &amp; 0xff; if (i == len) { out += CHARS.charAt(c1 &gt;&gt; 2); out += CHARS.charAt((c1 &amp; 0x3) &lt;&lt; 4); out += "=="; break; } c2 = str.charCodeAt(i++); if (i == len) { out += CHARS.charAt(c1 &gt;&gt; 2); out += CHARS.charAt(((c1 &amp; 0x3)&lt;&lt; 4) | ((c2 &amp; 0xF0) &gt;&gt; 4)); out += CHARS.charAt((c2 &amp; 0xF) &lt;&lt; 2); out += "="; break; } c3 = str.charCodeAt(i++); out += CHARS.charAt(c1 &gt;&gt; 2); out += CHARS.charAt(((c1 &amp; 0x3) &lt;&lt; 4) | ((c2 &amp; 0xF0) &gt;&gt; 4)); out += CHARS.charAt(((c2 &amp; 0xF) &lt;&lt; 2) | ((c3 &amp; 0xC0) &gt;&gt; 6)); out += CHARS.charAt(c3 &amp; 0x3F); } return out; } console.log(base64Encode(getBinary('http://www.google.fr/images/srpr/logo3w.png'))); </code></pre> <p>Hope this helps others as it did for me.</p>
    singulars
    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