Creating a BLOB from a Base64 string in JavaScript

后端 未结 12 2623
说谎
说谎 2020-11-21 04:24

I have Base64-encoded binary data in a string:

const contentType = \'image/png\';
const b64Data = \'iVBORw0KGg         


        
12条回答
  •  迷失自我
    2020-11-21 05:01

    For all browser support, especially on Android, perhaps you can add this:

    try{
        blob = new Blob(byteArrays, {type : contentType});
    }
    catch(e){
        // TypeError old Google Chrome and Firefox
        window.BlobBuilder = window.BlobBuilder ||
                             window.WebKitBlobBuilder ||
                             window.MozBlobBuilder ||
                             window.MSBlobBuilder;
        if(e.name == 'TypeError' && window.BlobBuilder){
            var bb = new BlobBuilder();
            bb.append(byteArrays);
            blob = bb.getBlob(contentType);
        }
        else if(e.name == "InvalidStateError"){
            // InvalidStateError (tested on FF13 WinXP)
            blob = new Blob(byteArrays, {type : contentType});
        }
        else{
            // We're screwed, blob constructor unsupported entirely
        }
    }
    

提交回复
热议问题