Skip to content

Instantly share code, notes, and snippets.

@suuuzi
Created April 27, 2016 10:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save suuuzi/06a3b0b6741e6a90d83548aa8ac9666a to your computer and use it in GitHub Desktop.
Save suuuzi/06a3b0b6741e6a90d83548aa8ac9666a to your computer and use it in GitHub Desktop.
function dataURItoBlob(dataURI) {
// convert base64/URLEncoded data component to raw binary data held in a string
var byteString;
if (dataURI.split(',')[0].indexOf('base64') >= 0)
byteString = atob(dataURI.split(',')[1]);
else
byteString = unescape(dataURI.split(',')[1]);
// separate out the mime component
var mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0];
// write the bytes of the string to a typed array
var ia = new Uint8Array(byteString.length);
for (var i = 0; i < byteString.length; i++) {
ia[i] = byteString.charCodeAt(i);
}
return new Blob([ia], {type:mimeString});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment