image to blob js code example

Example: javascript blob to image

// GET BLOB FROM XML REQUEST (JAVASCRIPT)
var xhr = new XMLHttpRequest();
xhr.open("GET", "http://localhost/image.jpg");
xhr.responseType = "blob";
xhr.onload = response;
xhr.send();
// MAKE A RESPONSE FUNCTION (JAVASCRIPT)
function response(e) {
   var urlCreator = window.URL || window.webkitURL;
   var imageUrl = urlCreator.createObjectURL(this.response);
   document.querySelector("#image").src = imageUrl;
}
// MAKE OUTPUT IMG (IN HTML)
<img id="image"/>
  // THIS IS OPEN SOURCE I DO NOT OWN THIS //