0

I have a snippet, that requests an image from a URL:

var toDataURL = url => fetch(url, {mode: "no-cors"})
  .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://www.google.hu/images/branding/googlelogo/2x/googlelogo_color_120x44dp.png')
  .then(dataUrl => {
    console.log('RESULT:', dataUrl)
  })

Code snippet is taken from this thread

This piece of code requests the google logo and it works just fine. I can see the successful image response in the network tab, I can see the image itself, also the base64 code is returned to the console as it should (you can try it by copying and pasting it to a Chrome console)

However, if I change the url to this: https://images.dog.ceo/breeds/husky/n02110185_12748.jpg

I don't get the base64 code back in the console. The request is successful, it is visible in the response, but the base64 is not returned.

I tried it with several images, with pngs most of the time it works, with jpegs never.

Are there some additional settings in the FileReader api I'm missing?

6
  • 1
    For jpeg, does modifying {mode: "no-cors"} to {mode: "no-cors",headers:{'content-type':"image/jpeg"}} work? Commented May 16, 2018 at 11:31
  • No, it does not. Commented May 16, 2018 at 11:38
  • If you look at the images.dog.ceo/breeds/husky/n02110185_12748.jpg source, the response header does not contain Allow-Access-Origin . So from another domain you will never get this client side. However if you open the location, then open the console and paste your code, you will see that the dataURL will be logged. Commented May 16, 2018 at 11:44
  • Ahh, you are right. I totally missed it. So if I want to get an image in base64 (the api I'm calling is returning urls) I have to make sure the webserver (their webserver) is configured with Allow-Access-Origin header for image requests? Commented May 16, 2018 at 11:52
  • yes. For images etc. I usually allow all domains, that is "*" wildcard Commented May 16, 2018 at 11:55

1 Answer 1

1

If you look at the images.dog.ceo/breeds/husky/n02110185_12748.jpg source, the response header does not contain Allow-Access-Control-Origin (https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Origin) .

So from another domain you will never get this client side. However if you open the location, then open the console and paste your code, you will see that the dataURL will be logged.

Consider white-listing domains and adding the appropriate header if possible, for some inspiration:

Access-Control-Allow-Origin Multiple Origin Domains?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.