Skip to content

Instantly share code, notes, and snippets.

@nicholasxjy
Created November 14, 2017 07:20
Show Gist options
  • Save nicholasxjy/6d12a96f4b027687ea0654876e3e0b2d to your computer and use it in GitHub Desktop.
Save nicholasxjy/6d12a96f4b027687ea0654876e3e0b2d to your computer and use it in GitHub Desktop.
base 64 to blob
export function b64toBlob(data, contentType = '', sliceSize = 512) {
const byteCharacters = atob(data)
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
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment