-
-
Save davoclavo/4424731 to your computer and use it in GitHub Desktop.
/* | |
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; | |
} |
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?
I got an error "InvalidStateError" when running this function in IE10 and IE11
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.
@davoclavo This code is licensed under the CC-BY-SA because it came from SA before they converted to MIT.
Here's an MIT version from @bebraw https://github.com/graingert/datauritoblob/blob/master/dataURItoBlob.js
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
is there a faster way?
a phone photo (13Mbytes) takes me 6 seconds to convert.
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.
Thanks for sharing
Thank you! This helped a lot. Appreciate you sharing