pbojinov
12/22/2015 - 9:27 PM

convert base64/URLEncoded data component to raw binary data held in a string http://stackoverflow.com/a/5100158/907388

convert base64/URLEncoded data component to raw binary data held in a string http://stackoverflow.com/a/5100158/907388

function dataURItoBlob(dataURI) {
    // convert base64/URLEncoded data component to raw binary data held in a string
    // http://stackoverflow.com/a/5100158/907388
    // inspired this pollyfill: https://github.com/blueimp/JavaScript-Canvas-to-Blob
    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});
}