Skip to content

Instantly share code, notes, and snippets.

@Nevon
Created February 24, 2013 15:17
Show Gist options
  • Save Nevon/5024181 to your computer and use it in GitHub Desktop.
Save Nevon/5024181 to your computer and use it in GitHub Desktop.
function(dataURI) {
var byteString;
//Convert base64 to raw binary data
if (dataURI.split(',')[0].indexOf('base64') >= 0) {
byteString = atob(dataURI.split(',')[1]);
} else {
byteString = unescape(dataURI.split(',')[1]);
}
//Get the mime type
var mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0];
//Write the binary data 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);
}
//Write the ArrayBuffer to a Blob
var blob;
try {
blob = new Blob([ia], {type: mimeString});
} catch (e) {
//The BlobBuilder API has been deprecated in favor
//of Blob. This is for backwards compatibility.
var BlobBuilder = window.WebKitBlobBuilder || window.MozBlobBuilder;
var bb = new BlobBuilder();
bb.append(ia);
blob = bb.getBlob(mimeString);
}
var image = document.createElement('img');
image.src = 'data:'+mimeString+';base64,'+btoa(blob);
document.body.appendChild(image);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment