Last active
December 23, 2020 07:45
-
-
Save dapize/d63f9fcab164790e8efabec085ed281d to your computer and use it in GitHub Desktop.
Base64 to Url
This file contains 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
function base64ToUrl (b64Data, type = 'image/png', sliceSize = 512) { | |
const byteCharacters = atob(b64Data); | |
const byteArrays = []; | |
const nByteCharacters = byteCharacters.length; | |
for (let offset = 0; offset < nByteCharacters; 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: type}); | |
return URL.createObjectURL(blob); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment