Last active
November 1, 2017 01:31
-
-
Save alexsanchezdev/723b7bac1271c264ca36c63cb59136eb to your computer and use it in GitHub Desktop.
Convert base64/URLEncoded data to raw binary data | Javascript
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
export default function dataURItoBlob(dataURI) { | |
var BASE64_MARKER = ';base64,'; | |
var parts; | |
var contentType; | |
var raw; | |
if (dataURI.indexOf(BASE64_MARKER) === -1) { | |
parts = dataURI.split(','); | |
contentType = parts[0].split(':')[1]; | |
raw = decodeURIComponent(parts[1]); | |
return new Blob([raw], { type: contentType }); | |
} else { | |
parts = dataURI.split(BASE64_MARKER); | |
contentType = parts[0].split(':')[1]; | |
raw = window.atob(parts[1]); | |
var rawLength = raw.length; | |
var uInt8Array = new Uint8Array(rawLength); | |
for (var i = 0; i < rawLength; ++i) { | |
uInt8Array[i] = raw.charCodeAt(i); | |
} | |
return new Blob([uInt8Array], { type: contentType }); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment