Skip to content

Instantly share code, notes, and snippets.

@dapize
Last active December 23, 2020 07:45
Show Gist options
  • Save dapize/d63f9fcab164790e8efabec085ed281d to your computer and use it in GitHub Desktop.
Save dapize/d63f9fcab164790e8efabec085ed281d to your computer and use it in GitHub Desktop.
Base64 to Url
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