Javascript: BlobBuilder polyfill


The BlobBuilder interface provides an easy way to construct Blob objects. Just create a BlobBuilder and append chunks of data to it by calling the append() method. When you’re done building your blob, call getBlob() to retrieve a Blob containing the data you sent into the blob builder.

This feature is obsolete. Although it may still work in some browsers, its use is discouraged since it could be removed at any time. Try to avoid using it.

BlobBuilder.js

/**
 * @author www.tutorialspots.com
 * @copyright 2019
 */
    
class _BlobBuilder{     
    constructor() {
        this.blob = new Blob()
    }
    append(data, endings){
        endings = endings || "transparent";
        if(this.blob.size==0)
            this.blob = new Blob([data],{endings:endings})
        else
            this.blob = new Blob([this.blob,data],{endings:endings})
    }     
    getBlob(contentType){
        if(contentType!=undefined) 
            this.blob.type = contentType
        return this.blob;
    }
    getFile(name, contentType){
        if(contentType!=undefined)
            return new File(this.blob, name,{type: contentType})
        
        return new File(this.blob, name)
    }
}   

if("undefined" == typeof MozBlobBuilder && "undefined" == typeof WebKitBlobBuilder &&  "undefined" == typeof MSBlobBuilder)
    WebKitBlobBuilder = _BlobBuilder; 
else
    BlobBuilder = BlobBuilder || WebKitBlobBuilder || MozBlobBuilder || MSBlobBuilder;
    
if("undefined" == typeof BlobBuilder)
    BlobBuilder = _BlobBuilder;  

Download source code

1 Comment

Leave a Reply