question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

fetch base64 string failed on Safari

See original GitHub issue

here is my code, work well on Chrome56:

function dataURItoFile(dataURI, filename) {
  const mimeType = dataURI.split(',')[0].split(':')[1].split(';')[0]
  return fetch(dataURI)
    .then(res => res.arrayBuffer())
    .then(buf => new File([buf], filename, { type: mimeType }))
}

dataURI: base64 string

but on Safari10:

XMLHttpRequest cannot load data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAABaAAAAQACAYAAAAXy/6SAAAAAXNSR0IArs4c6QAAQABJREFUeAHsXQlAVcUa/tgXUQRBRVLQm6KIIhpaUmkmUoqllWgmlgaZTzKXXF+Z+so1rQif+iA3zFwqNckyjFzCXFJEFEXFVERQUARRdnjfnLtw2ZQ2QJ1fuWfOnDlzZr5zzpyZb/75f4O2bT1KIEUiIBGQCEgEqkTg1KmYKo/dzwf4fbifqyfrJhGQCEgEJAISAYmAREAiIBGQCEgEJAISAYlADSBgWAPXkJeQCEgEJAISAYmAREAiIBGQCEgEJAISAYmAREAiIBGQCEgEJAISgQcQAUlAP4A3XVZZIiARkAhIBCQCEgGJgERAIiARkAhIBCQCEgGJgERAIiARkAjUBAKSgK4JlOU1JAISAYmAREAiIBGQCEgEJAISAYmAREAiIBGQCEgEJAISAYnAA4iAJKAfwJsuqywRkAhIBCQCEgGJgERAIiARkAhIBCQCEgGJgERAIiARkAhIBGoCAUlA1wTK8hoSAYmAREAiIBGQCEgEJAISAYmAREAiIBGQCEgEJAISAYmAROABREAS0A/gTZdVlghIBCQCEgGJgERAIiARkAhIBCQCEgGJgERAIiARkAhIBCQCNYGAJKBrAmV5DYmAREAiIBGQCEgEJAISAYmAREAiIBGQCEgEJAISAYmAREAi8AAiIAnoB/CmyypLBCQCEgGJgERAIiARkAhIBCQCEgGJgERAIiARkAhIBCQCEoGaQMC4Ji4ir/FgI2DYsBGMW7WDkaqtsjVu1RYGVg1QfPUyiq4ko+jS78g/8DMKz54ASkoebLBk7SUCEgGJgERAIiARkAhIBCQCEgGJgERAIiARkAhIBCQC9xECBm3bekjG7z66oXWpKkaOzrAcOgamjz1drWIV/p6A7MXTUJR8vlrpZSKJQE0hcOpUTE1dqk5dh9+HOlUeWRiJgERAIiARkAhIBCQCEgGJgERAIiARkAhIBO49BKQG9L13z+p+iY2MYPHCSFj6BQJG1X/Eis6dQtHli3W/frKEEgGJgERAIiARkAhIBCQCEgGJgERAIiARkAhIBCQCEgGJQLUQqD47WK3sZKIHHQEDE1NYTVoI00ee+ENQ5O3cguylH9AER/EfOk8mlghIBCQCEgGJgERAIiARkAhIBCQCEgGJgERAIiARkAhIBOouAtIJYd29N/dkyeqNmVG3yWf3HujvZVMtbO1UTlDxz65aqUsTefX3gXvprgxJBCQCEgGJgERAIiARkAhIBCQCEgGJgERAIiARkAhIBB5YBKQG9AN76//+ipv7vASzJ/v+oYzzdm6m5vOHf1Lz2RNhUYvQ5vZZ/LJvF37+bhsiYzOquL4rpi99Dy/3dIExCvF65BpMHBuCxCpSQxWIbyNGQ1DVx8NGYdCiQ7qUXsMnYFzAS3CzuYz3O7yEjbojDNgNxtwFU2C/4D9IifkGgUPnVX0N/fNkWCIgEZAISAQkAhIBiYBEQCIgEZAISAQkAn8RgTYqR7w1sj+6d3WFpYXZX8ytbp5eWFiEmLhELFu9Hft+O1k3CylLJRGQCJRBQBLQZeCQO38WAUO7prB8dfwfOl1tduPPks9AYNgH8HKw4jU....

so, is that a bug?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
jimmywartingcommented, Apr 24, 2017

The problem

It boils down to this code:

url = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=="
xhr = new XMLHttpRequest
xhr.open('GET', url)
xhr.send()

skarmavbild 2017-03-25 kl 21 18 16

When sortering the url i saw the full error message:

XMLHttpRequest cannot load < URL >. Cross origin requests are only supported for HTTP.

I tested the same code in Safari TP and it worked just fine. So did the native fetch version of your code also.

Workaround

Use any other way to make a File out of a base64. Make your pick: http://stackoverflow.com/questions/16245767/creating-a-blob-from-a-base64-string-in-javascript

Code review

using blob instead of arraybuffer yields a better result, you don’t have to get the mimeType from the base64 since it will be included when you return the blob. Beside blob may also be a better choice for you since you can’t construct files on all browsers (see note 2 on caniuse.com)

  return fetch(dataURI)
    .then(res => res.blob())
    .then(blob => new File([blob], filename, { type: blob.type }))

What can the polyfill do to fix it?

Parse the url, see if it’s a data URI and if it’s convert the data URI to a blob internally without using xhr

0reactions
mislavcommented, Sep 20, 2017

I would agree on a workaround if it was easy (e.g. 2–3 lines of code). However, since the logic to convert a base64 would inflate the size of this polyfill, I don’t think this is something that we should be working around of, and instead it should be the responsibility of the user to avoid using fetch() for this on unsupporting browsers.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Base64 image tag in safari did not showed up - Stack Overflow
I know this question is pretty old but I've recently faced a similar problem on iOS' safari, and the problem seems to be...
Read more >
Safari trunctates base64 string re… | Apple Developer Forums
I have a page which allows the user to upload an image. The image displays properly in Safari. The image data is in...
Read more >
HTMLCanvasElement.toDataURL() - Web APIs | MDN
A string containing the requested data URL. If the height or width of the canvas is 0 or larger than the maximum canvas...
Read more >
django.contrib.messages CookieStorage failing silently in ...
django.contrib.messages CookieStorage failing silently in safari when comma is used ... Safari browser, but I fixed it by adding a base64 encode and...
Read more >
fetch-base64-in-browser - npm
fetch -base64-in-browser fetches binary files on the web and converts them to either a Base64 string or a data uri (which is a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found