Last active
February 22, 2023 21:04
-
-
Save robert-hoffmann/58b712b7221fed3f512f9fc53683504a to your computer and use it in GitHub Desktop.
download img from web and get base64 data
This file contains hidden or 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
| <img/> |
This file contains hidden or 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
| 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