Skip to content

Instantly share code, notes, and snippets.

@gartenfeld
Created October 27, 2015 14:13
Show Gist options
  • Select an option

  • Save gartenfeld/95ab367dbe8fee3925aa to your computer and use it in GitHub Desktop.

Select an option

Save gartenfeld/95ab367dbe8fee3925aa to your computer and use it in GitHub Desktop.
Load an image and convert it to a string.
var xhr = new XMLHttpRequest();
xhr.responseType = 'arraybuffer';
xhr.onload = function() {
if (this.status == 200) {
var buffer = new Uint8Array(this.response);
var i = buffer.length;
var binary = new Array(i);
while (i--) {
binary[i] = String.fromCharCode(buffer[i]);
}
var base64 = window.btoa(binary.join(''));
}
};
xhr.open('GET', URL, true);
xhr.send();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment