Skip to content

Instantly share code, notes, and snippets.

@davoclavo
Last active May 26, 2023 14:45
Show Gist options
  • Star 48 You must be signed in to star a gist
  • Fork 12 You must be signed in to fork a gist
  • Save davoclavo/4424731 to your computer and use it in GitHub Desktop.
Save davoclavo/4424731 to your computer and use it in GitHub Desktop.
Convert dataURI to Blob so large images do not crash the browser. Based on: http://stackoverflow.com/questions/10412299 and http://stackoverflow.com/questions/6850276
/*
The MIT License (MIT)
Copyright (c) 2016 David Gomez-Urquiza
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
function dataURItoBlob(dataURI) {
// convert base64 to raw binary data held in a string
var byteString = atob(dataURI.split(',')[1]);
// separate out the mime component
var mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0];
// write the bytes of the string to an ArrayBuffer
var arrayBuffer = new ArrayBuffer(byteString.length);
var _ia = new Uint8Array(arrayBuffer);
for (var i = 0; i < byteString.length; i++) {
_ia[i] = byteString.charCodeAt(i);
}
var dataView = new DataView(arrayBuffer);
var blob = new Blob([dataView], { type: mimeString });
return blob;
}
@doubletaketech
Copy link

Thank you! This helped a lot. Appreciate you sharing

@confile
Copy link

confile commented Mar 20, 2014

This work but when you look here http://caniuse.com/#feat=bloburls Blob() is sometimes supported with the webkit prefix. I could not find an example hat to fix it. I mean I need something like:

var _URL = URL || webkitURL;

The preview vor the Blob(). I don't know how to include the webkit fallback. Can you give me some help please?

@peterpham
Copy link

I got an error "InvalidStateError" when running this function in IE10 and IE11

@peterpham
Copy link

Never mind. After changing this
var blob = new Blob([dataView], { type: mimeString });
to this
var blob = new Blob([dataView.buffer], { type: mimeString });

Everything works again.

@graingert
Copy link

@davoclavo This code is licensed under the CC-BY-SA because it came from SA before they converted to MIT.

@graingert
Copy link

@bebraw and @mattcg I'd like to use this code in some proprietary software. Would you mind re-licensing under MIT, please?

@graingert
Copy link

@chandrasekarb
Copy link

hi I’m try this program but i have error in
var byteString = atob(dataURI.split(',')[1]); this line

{ [ReferenceError: atob is not defined] expose: true, statusCode: 400, status: 400 }

please give any solution

@windsome
Copy link

windsome commented Mar 2, 2017

is there a faster way?
a phone photo (13Mbytes) takes me 6 seconds to convert.

@farheen14aslam
Copy link

Hello .. I am trying to upload a file of size 80 MB, but my app crashes executing "var byteString = atob(dataURI.split(',')[1]);"
Is there any other way to write the file using filewriter when the data uri is too large.

@Sardorjon
Copy link

Thanks for sharing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment