附加Blob数据 [英] Appending Blob data
问题描述
是否有在 JavaScript中附加blob数据的功能
我目前使用以下方法:
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" });
Chrome中没有 BlobBuilder
功能。
And BlobBuilder
function is not available in Chrome.
推荐答案
Blob s是不可变的所以你不能在制作之后改变它。构建一个将数据附加到现有blob的新Blob(正如您在初始问题中所写的那样)是一个很好的解决方案。
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.
如果每次附加零件时都不需要使用Blob,则只需跟踪零件阵列即可。然后你可以不断附加到数组,然后在你需要的时候构造Blob。
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();
这篇关于附加Blob数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!