Skip to content

Instantly share code, notes, and snippets.

@robert-hoffmann
Last active February 22, 2023 21:04
Show Gist options
  • Select an option

  • Save robert-hoffmann/58b712b7221fed3f512f9fc53683504a to your computer and use it in GitHub Desktop.

Select an option

Save robert-hoffmann/58b712b7221fed3f512f9fc53683504a to your computer and use it in GitHub Desktop.
download img from web and get base64 data
async function getBase64FromUrl(url) {
const data = await fetch(url);
const blob = await data.blob();
return new Promise((resolve) => {
const reader = new FileReader();
reader.readAsDataURL(blob);
reader.onloadend = () => {
const base64data = reader.result;
resolve(base64data);
}
});
}
let url = "https://picsum.photos/200/300";
getBase64FromUrl(url)
.then(data => {
console.log(data);
document.querySelector("img").src = data;
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment