Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript. Append to blob

Because BlobBuilder is deprecated, we have to use Blob, so instead of

var bb = new (window.MozBlobBuilder || window.WebKitBlobBuilder || window.BlobBuilder)();
bb.append(data);
var blob = bb.getBlob();

We do

var blob = new Blob([data]);

That is ok, but what if I want to append Data to same blob more times? Something like:

for(var i=0;i<10;i++){
    bb.append(" "+i);
}

How to do it without BlobBuilder? Thanks all!

like image 626
RuSsCiTy Avatar asked Feb 02 '15 15:02

RuSsCiTy


People also ask

What is the use of append in blob?

Appends the contents of the specified JavaScript object to the Blob being built. If the value you specify isn't a Blob, ArrayBuffer, or String, the value is coerced to a string before being appended to the blob. void append(in ArrayBuffer data); void append(in Blob data); void append(in String data, [ optional] in String endings);

What is a blob in JavaScript?

In the browser, there are additional higher-level objects, described in File API, in particular Blob. Blob consists of an optional string type (a MIME-type usually), plus blobParts – a sequence of other Blob objects, strings and BufferSource.

How do I create a blobbuilder in Java?

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.

How do I change the type of a blob?

The storage service offers three types of blobs, block blobs, append blobs, and page blobs. You specify the blob type when you create the blob. Once the blob has been created, its type cannot be changed, and it can be updated only by using operations appropriate for that blob type, i.e.,...


2 Answers

Thanks to minitech♦!

You answer moved me to answer my question. So what you show is again working with a variable and at the end write it to blob. I wanted so said update a blob, or with other words append to blob. So you can do it on this way:

var blob = new Blob([],  {type: "text/plain"});
for (var i=0; i<10; i++){
    blob = new Blob([blob," "+i], {type: "text/plain"});
}

Here is Fiddle: http://jsfiddle.net/yuM2N/111/

like image 122
RuSsCiTy Avatar answered Oct 07 '22 18:10

RuSsCiTy


[data] is an array of parts, so you can just append to an array:

var parts = [];

for (var i = 0; i < 10; i++) {
    parts.push(" " + i);
}

var blob = new Blob(parts);
like image 27
Ry- Avatar answered Oct 07 '22 19:10

Ry-