Skip to content

Instantly share code, notes, and snippets.

@aautar
Created October 19, 2019 21:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aautar/e77ceaee0709b79e7fd9ac5bbddfaf5d to your computer and use it in GitHub Desktop.
Save aautar/e77ceaee0709b79e7fd9ac5bbddfaf5d to your computer and use it in GitHub Desktop.
Convert a data URL, with base64 encoded data, to a Blob with binary data
const mime = MIMEFromDataURI.getMIMEType(e.data); // using https://github.com/aautar/mime-from-data-uri
const dataB64 = e.data.split(',')[1];
const imageDataStr = window.atob(dataB64);
const imageDataBinaryArr = new Uint8Array(new ArrayBuffer(imageDataStr.length));
for (let i=0; i<imageDataStr.length; i++) {
imageDataBinaryArr[i] = imageDataStr.charCodeAt(i);
}
const blob = new Blob([imageDataBinaryArr], {type: mime});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment