'Is there any way to get base64 encoded string of any remote file in javascript?

I have an image url:

url = "https://wallpaperaccess.com/full/3678503.png";

Is there any way to get base64 encoded string of that remote file in javascript?



Solution 1:[1]

I found on SO this codesnipet to achieve this. The only problem would be that you will run into CORS Problem. If you are the owner of the page which hosted the image you can configure up to avoid CORS.

const toDataURL = url => fetch(url)
  .then(response => response.blob())
  .then(blob => new Promise((resolve, reject) => {
    const reader = new FileReader()
    reader.onloadend = () => resolve(reader.result)
    reader.onerror = reject
    reader.readAsDataURL(blob)
  }))


toDataURL('https://wallpaperaccess.com/full/3678503.png')
  .then(dataUrl => {
    console.log('RESULT:', dataUrl)
  })

And here the Link where i found the source: https://stackoverflow.com/a/20285053/14807111

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Maik Lowrey