Skip to content

Instantly share code, notes, and snippets.

@tonghoangvu
Created December 27, 2022 15:26
Show Gist options
  • Save tonghoangvu/5308a404986f7829e8b8c58bcc53bce5 to your computer and use it in GitHub Desktop.
Save tonghoangvu/5308a404986f7829e8b8c58bcc53bce5 to your computer and use it in GitHub Desktop.
Load image blob by Fetch API
const img = document.getElementById('img')
const JWT = 'ey...'
const API_URL = 'http://localhost:8080'
const IMG_NAME = 'ABC.png'
// https://stackoverflow.com/a/50248437/13779659
fetch(`${API_URL}/secure-images/${IMG_NAME}`, {
headers: {
Authorization: `Bearer ${JWT}`
}
})
.then(res => res.blob())
.then(imgBlob => {
// Then create a local URL for that image and print it
const imageObjectURL = URL.createObjectURL(imgBlob)
console.log(imageObjectURL)
img.src = imageObjectURL
})
.catch(err => console.log(err));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment