Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Marceloalves6/d1f590437f7fe45ac6832e5177c84be6 to your computer and use it in GitHub Desktop.
Save Marceloalves6/d1f590437f7fe45ac6832e5177c84be6 to your computer and use it in GitHub Desktop.
private base64toBlob(base64Data, contentType): Blob {
contentType = contentType || '';
const sliceSize = 1024;
const byteCharacters = atob(base64Data);
const bytesLength = byteCharacters.length;
const slicesCount = Math.ceil(bytesLength / sliceSize);
const byteArrays = new Array(slicesCount);
for (let sliceIndex = 0; sliceIndex < slicesCount; ++sliceIndex) {
const begin = sliceIndex * sliceSize;
const end = Math.min(begin + sliceSize, bytesLength);
const bytes = new Array(end - begin);
for (let offset = begin, i = 0; offset < end; ++i, ++offset) {
bytes[i] = byteCharacters[offset].charCodeAt(0);
}
byteArrays[sliceIndex] = new Uint8Array(bytes);
}
return new Blob(byteArrays, { type: contentType });
}
@w8ze-devel
Copy link

Great code. Thanks. Save me a lot of time.

I just replace the call to atob with

Buffer.from(base64Data, 'base64').toString('latin1')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment