Created
December 27, 2022 15:26
-
-
Save tonghoangvu/5308a404986f7829e8b8c58bcc53bce5 to your computer and use it in GitHub Desktop.
Load image blob by Fetch API
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
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