Skip to content

Instantly share code, notes, and snippets.

@lucascompython
Created February 25, 2022 21:58
Show Gist options
  • Save lucascompython/ded0dcde5d6d110658d58826985e202d to your computer and use it in GitHub Desktop.
Save lucascompython/ded0dcde5d6d110658d58826985e202d to your computer and use it in GitHub Desktop.
This is a javascript function to convert a given base64 string to a blob.
const b64toBlob = (b64Data, contentType='', sliceSize=512) => {
const byteCharacters = atob(b64Data);
const byteArrays = [];
for (let offset = 0; offset < byteCharacters.length; offset += sliceSize) {
const slice = byteCharacters.slice(offset, offset + sliceSize);
const byteNumbers = new Array(slice.length);
for (let i = 0; i < slice.length; i++) {
byteNumbers[i] = slice.charCodeAt(i);
}
const byteArray = new Uint8Array(byteNumbers);
byteArrays.push(byteArray);
}
const blob = new Blob(byteArrays, {type: contentType});
return blob;
}
export default b64toBlob;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment