【问题标题】:Size of Blob created from Uint8Array gives nonsense values [duplicate]从 Uint8Array 创建的 Blob 大小给出了无意义的值 [重复]
【发布时间】:2018-05-03 20:03:45
【问题描述】:

所以我想知道为什么这个 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 items 可以处理从 0 到 255 的值。)

此外,更改 x[0]x[1] 似乎会更改 new Blob(x).size。但是,x.byteLength 给了我预期的结果。

即使我到处搜索,我也找不到任何解释。

【问题讨论】:

    标签: javascript blob typed-arrays uint8array


    【解决方案1】:

    Blob constructor 采用缓冲区数组,而不是单个缓冲区。您当前的代码与

    相同
    new Blob(["255", "10"])
    

    这就是为什么你会得到5 的大小。你需要写

    var x = new Uint8Array([255, 10]);
    new Blob([x])
    //       ^ ^
    

    【讨论】:

    • 你需要传递一个Uint8Arrays的数组。
    猜你喜欢
    • 2022-10-06
    • 2013-07-26
    • 2018-09-08
    • 1970-01-01
    • 1970-01-01
    • 2015-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多