Creating a BLOB from a Base64 string in JavaScript

后端 未结 12 2663
说谎
说谎 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:08

    I noticed that Internet Explorer 11 gets incredibly slow when slicing the data like Jeremy suggested. This is true for Chrome, but Internet Explorer seems to have a problem when passing the sliced data to the Blob-Constructor. On my machine, passing 5 MB of data makes Internet Explorer crash and memory consumption is going through the roof. Chrome creates the blob in no time.

    Run this code for a comparison:

    var byteArrays = [],
        megaBytes = 2,
        byteArray = new Uint8Array(megaBytes*1024*1024),
        block,
        blobSlowOnIE, blobFastOnIE,
        i;
    
    for (i = 0; i < (megaBytes*1024); i++) {
        block = new Uint8Array(1024);
        byteArrays.push(block);
    }
    
    //debugger;
    
    console.profile("No Slices");
    blobSlowOnIE = new Blob(byteArrays, { type: 'text/plain'});
    console.profileEnd();
    
    console.profile("Slices");
    blobFastOnIE = new Blob([byteArray], { type: 'text/plain'});
    console.profileEnd();
    

    So I decided to include both methods described by Jeremy in one function. Credits go to him for this.

    function base64toBlob(base64Data, contentType, sliceSize) {
    
        var byteCharacters,
            byteArray,
            byteNumbers,
            blobData,
            blob;
    
        contentType = contentType || '';
    
        byteCharacters = atob(base64Data);
    
        // Get BLOB data sliced or not
        blobData = sliceSize ? getBlobDataSliced() : getBlobDataAtOnce();
    
        blob = new Blob(blobData, { type: contentType });
    
        return blob;
    
    
        /*
         * Get BLOB data in one slice.
         * => Fast in Internet Explorer on new Blob(...)
         */
        function getBlobDataAtOnce() {
            byteNumbers = new Array(byteCharacters.length);
    
            for (var i = 0; i < byteCharacters.length; i++) {
                byteNumbers[i] = byteCharacters.charCodeAt(i);
            }
    
            byteArray = new Uint8Array(byteNumbers);
    
            return [byteArray];
        }
    
        /*
         * Get BLOB data in multiple slices.
         * => Slow in Internet Explorer on new Blob(...)
         */
        function getBlobDataSliced() {
    
            var slice,
                byteArrays = [];
    
            for (var offset = 0; offset < byteCharacters.length; offset += sliceSize) {
                slice = byteCharacters.slice(offset, offset + sliceSize);
    
                byteNumbers = new Array(slice.length);
    
                for (var i = 0; i < slice.length; i++) {
                    byteNumbers[i] = slice.charCodeAt(i);
                }
    
                byteArray = new Uint8Array(byteNumbers);
    
                // Add slice
                byteArrays.push(byteArray);
            }
    
            return byteArrays;
        }
    }
    

提交回复
热议问题