Skip to content

Instantly share code, notes, and snippets.

@getdave
Created September 15, 2017 10:00
Show Gist options
  • Save getdave/8f01204c8d5d5d70d4046ab31333589c to your computer and use it in GitHub Desktop.
Save getdave/8f01204c8d5d5d70d4046ab31333589c to your computer and use it in GitHub Desktop.
Fetch an image using the Fetch API
var myImage = 'someimage.jpg';
fetch(myImage)
.then(response => response.blob()) //To extract the image body content from the response, we use the blob() method
.then(myBlob => {
// An objectURL is then created from the extracted Blob, which is then inserted into the img.
const objectURL = URL.createObjectURL(myBlob);
myImage.src = objectURL;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment