Created
January 3, 2013 11:45
-
-
Save sang4lv/4442930 to your computer and use it in GitHub Desktop.
Ajax Level 2: Handling BLOB Data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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