Created
May 14, 2015 08:26
-
-
Save madis/f8820646e3696d655fb4 to your computer and use it in GitHub Desktop.
Data URI to Blob
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
// From http://stackoverflow.com/questions/6850276/how-to-convert-dataurl-to-file-object-in-javascript | |
function dataURItoBlob(dataURI) { | |
if(typeof dataURI !== 'string'){ | |
throw new Error('Invalid argument: dataURI must be a string'); | |
} | |
dataURI = dataURI.split(','); | |
var type = dataURI[0].split(':')[1].split(';')[0], | |
byteString = atob(dataURI[1]), | |
byteStringLength = byteString.length, | |
arrayBuffer = new ArrayBuffer(byteStringLength), | |
intArray = new Uint8Array(arrayBuffer); | |
for (var i = 0; i < byteStringLength; i++) { | |
intArray[i] = byteString.charCodeAt(i); | |
} | |
return new Blob([intArray], { | |
type: type | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment