Created
September 12, 2016 13:19
-
-
Save huozhi/4c562e9d56e5ab280ec5459c8c8947db to your computer and use it in GitHub Desktop.
convert base64 to blob object (from stackoverflow)
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
export const dataURItoBlob = (dataURI) => { | |
// convert base64/URLEncoded data component to raw binary data held in a string | |
let byteString | |
if (dataURI.split(',')[0].indexOf('base64') >= 0) { | |
byteString = atob(dataURI.split(',')[1]) | |
} else { | |
byteString = unescape(dataURI.split(',')[1]) | |
} | |
// separate out the mime component | |
const mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0] | |
// write the bytes of the string to a typed array | |
const ia = new Uint8Array(byteString.length) | |
for (let i = 0; i < byteString.length; i++) { | |
ia[i] = byteString.charCodeAt(i) | |
} | |
return new Blob([ia], {type:mimeString}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment