Created
February 15, 2019 17:12
-
-
Save Marceloalves6/d1f590437f7fe45ac6832e5177c84be6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Great code. Thanks. Save me a lot of time.
I just replace the call to atob with
Buffer.from(base64Data, 'base64').toString('latin1')