所以我想知道为什么这个 Blob
对象有一个 size
共 5 条:
var x = new Uint8Array(2);
x[0] = 255;
x[1] = 10;
console.log("Typed array length is " + x.length + ".")
console.log("Typed array byte length is " + x.byteLength + ".")
console.log("Blob size is " + new Blob(x).size + ' "bytes".')
对我来说这没有任何意义,因为 Uint8Array
元素可以存储在一个字节内。 ( Uint8Array
项目可以处理 0 到 255 之间的值。)
此外,更改 x[0]
或x[1]
似乎改变了new Blob(x).size
。 x.byteLength
然而,给了我预期的结果。
尽管我已经到处搜索,但我找不到任何解释。
最佳答案
Blob
constructor采用缓冲区数组,而不是单个缓冲区。您当前的代码的工作方式与
new Blob(["255", "10"])
这就是为什么你得到的大小为5
。你需要写
var x = new Uint8Array([255, 10]);
new Blob([x])
// ^ ^
关于javascript - 从 Uint8Array 创建的 Blob 的大小给出了无意义的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50163041/