You could use fetch instead of XMLHttpRequest, and instead of having a callback you can just make use of promises, like this:
fetch
XMLHttpRequest
const toDataURL = url => fetch(url) .then(response => response.blob()) .then( blob => new Promise((resolve, reject) => Object.assign(new FileReader(), { onloadend: ({ target }) => resolve(target.result), onerror: ({ target }) => reject(target.error), }).readAsDataURL(blob), ), );
And then you use it like this:
toDataURL("https://Gurimg.sh20raj.repl.co/logo.jpg") .then(console.log) .catch(console.error);
Consider that CORS is a factor here, so with either method you file fetching might get blocked unless your origin is allowed.
Cheers!
Nice π And we have WebScrapperJS to pass cors...
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
You could use
fetchinstead ofXMLHttpRequest, and instead of having a callback you can just make use of promises, like this:And then you use it like this:
Consider that CORS is a factor here, so with either method you file fetching might get blocked unless your origin is allowed.
Cheers!
Nice π
And we have WebScrapperJS to pass cors...