Skip to content

Instantly share code, notes, and snippets.

@sang4lv
Created January 3, 2013 11:45
Show Gist options
  • Save sang4lv/4442930 to your computer and use it in GitHub Desktop.
Save sang4lv/4442930 to your computer and use it in GitHub Desktop.
Ajax Level 2: Handling BLOB Data
window.URL = window.URL || window.webkitURL; // Take care of vendor prefixes.
var xhr = new XMLHttpRequest();
xhr.open('GET', '/path/to/image.png', true);
xhr.responseType = 'blob';
xhr.onload = function(e) {
if (this.status == 200) {
var blob = this.response;
var img = document.createElement('img');
img.src = window.URL.createObjectURL(blob);
img.onload = function(e) {
window.URL.revokeObjectURL(img.src); // Clean up after yourself.
};
document.body.appendChild(img);
}
};
xhr.send();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment