Writing Byte Array To Binary File Javascript

老子叫甜甜 提交于 2019-12-01 11:27:16

The Blob constructor does expect an array of typed arrays to be concatenated, but you are passing only a single Uint8Array into it. This will probably be interpreted as (should I say, "casted to"?) an array of DOM-strings - that's where your numbers are coming from.

A quickfix would be to use

new Blob([randomData], {type: "application/octet-stream"})
//       ^          ^

but I would suggest to either do

var randomData = [];
// while randomData.length < 308
    var bytes = new Uint8Array(4);
    for (var i=4; i--; ) {
        bytes[i] = longRandomNumber & (255);
        longRandomNumber = longRandomNumber >> 8
    }
    randomData.push(bytes);

var blob = new Blob(randomData, {type: "application/octet-stream"});

or not use those 4-byte bytes arrays at all:

var randomData = new Uint8Array(1232),
    count = 0;
// while count < randomData.length
    for (var i=4; i--; ) {
        randomData[count++] = longRandomNumber & (255);
        longRandomNumber = longRandomNumber >> 8
    }

var blob = new Blob([randomData], {type: "application/octet-stream"});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!