Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Appending Blob data

Is there a function for appending blob data in JavaScript I currently use the following approach:

var bb = new Blob(["Hello world, 2"], { type: "text/plain" }); bb = new Blob([bb, ",another data"], { type: "text/plain" }); 

And BlobBuilder function is not available in Chrome.

like image 829
Deni Spasovski Avatar asked Apr 12 '13 11:04

Deni Spasovski


People also ask

What is an append blob?

An append blob is composed of blocks and is optimized for append operations. When you modify an append blob, blocks are added to the end of the blob only, via the Append Block operation. Updating or deleting of existing blocks is not supported. Unlike a block blob, an append blob does not expose its block IDs.

What is the use of blob in JavaScript?

The Blob object represents a blob, which is a file-like object of immutable, raw data; they can be read as text or binary data, or converted into a ReadableStream so its methods can be used for processing the data. Blobs can represent data that isn't necessarily in a JavaScript-native format.


1 Answers

Blobs are "immutable" so you can't change one after making it. Constructing a new Blob that appends the data to an existing blob (as you wrote in your initial question) is a good solution.

If you don't need to use the Blob each time you append a part, you can just track an array of parts. Then you can continually append to the array and then construct the Blob at the end when you need it.

var MyBlobBuilder = function() {   this.parts = []; }  MyBlobBuilder.prototype.append = function(part) {   this.parts.push(part);   this.blob = undefined; // Invalidate the blob };  MyBlobBuilder.prototype.getBlob = function() {   if (!this.blob) {     this.blob = new Blob(this.parts, { type: "text/plain" });   }   return this.blob; };  var myBlobBuilder = new MyBlobBuilder();  myBlobBuilder.append("Hello world, 2");  // Other stuff ...   myBlobBuilder.append(",another data"); var bb = myBlobBuilder.getBlob(); 
like image 175
nkron Avatar answered Oct 06 '22 00:10

nkron