fetch base64 string failed on Safari
See original GitHub issuehere 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:
- Created 7 years ago
- Comments:5 (2 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
The problem
It boils down to this code:
When sortering the url i saw the full error message:
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)
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
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.