-
-
Save graingert/a252a75e0407955d443c63544ccbc4ee 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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment