简体   繁体   English

Javascript:Uint16Array(length)返回无效的参数

[英]Javascript: Uint16Array(length) returns Invalid argument

I'm trying to read a file in order to perform certain actions on the binary data before sending it to a server. 我正在尝试读取文件,以便在将二进制数据发送到服务器之前对二进制数据执行某些操作。

At a certain point I'm trying to convert the data returned by FileReader.readAsArrayBuffer() to an Uint16Array(). 在某些时候,我试图将FileReader.readAsArrayBuffer()返回的数据转换为Uint16Array()。 However upon doing so the code allocating the array fails with: 'Error: invalid arguments'. 但是,这样做后,分配数组的代码将失败,并显示:“错误:无效参数”。 I need the data to be a hex string representing the entire binary. 我需要数据是代表整个二进制的十六进制字符串。

This is the code I'm using: 这是我正在使用的代码:

function HexToHexString(ByteBuffer)
{
    //Similar constructs like: 'var Array = new Uint16Array(ByteBuffer);' also fail
    var View = new DataView(ByteBuffer);
    var Array = new Uint16Array((ByteBuffer.byteLength / 2));  // <- this line fails

    for(var i = 0; i < Array.length; i++)
    {
        Array[i] = View.getUint16(i*2);
    }

    return String.fromCharCode.apply(null, Array);
}

function OnReadFileCompletion(FileReadEvent)
{
    if(FileReadEvent.target.readyState == FileReader.DONE)
    {
        // Debug code, will be replaced:
        document.getElementById('byte_content').textContent = HexToHexString(FileReadEvent.target.result);
        //FileReadEvent.target.result;
    }
}

function ReadFile(File, ResultFunction)
{
    var Reader = new FileReader();

    Reader.onloadend = ResultFunction;

    Reader.readAsArrayBuffer(File.slice(0, File.size - 1));
}

File Is a file object, ResultFunction is OnReadFileCompletion(), ByteBuffer is an '[object ArrayBuffer]'. File是文件对象,ResultFunction是OnReadFileCompletion(),ByteBuffer是'[object ArrayBuffer]'。

When I output the size of the ArrayBuffer it matches the size of the file (82kb). 当我输出ArrayBuffer的大小时,它与文件的大小(82kb)相匹配。 I'm on firefox 32 with no plugins installed. 我在没有安装插件的firefox 32上。

I'm not a javascript programmer, does anyone know what I'm doing wrong? 我不是JavaScript程序员,有人知道我做错了吗?

Edit1: 编辑1:

It appears to have something to-do with the size of the file I'm trying to read, using a 1kb text file appears to work while a 82kb binary file does not. 它似乎与我要读取的文件大小有关,使用1kb的文本文件似乎可以工作,而使用82kb的二进制文件则不行。

Edit2 编辑2

I spoke too soon, perhaps it has something to do with file types. 我讲得太早了,也许与文件类型有关。 An image file of 200kb works, while an executable of 82 does not. 200kb的图像文件有效,而82的可执行文件无效。

It appears that javascript does not allow executable files to be accesed this way, does anybody know of any way where I could possibly access the data in hex form? 看来javascript不允许以这种方式访问​​可执行文件,有人知道我可以以十六进制形式访问数据的任何方式吗?

尝试使用.length而不是byteLength

I've hacked together code that works for me, I don't know why this works or what I did wrong the other time. 我已经一起破解了适用于我的代码,不知道为什么这样做或在其他时间我做错了什么。 But it works. 但这有效。

function ApplyPadding(Number, PaddingLength)
{
    var s = Number + "";
    while (s.length < PaddingLength)
        s = "0" + s;
    return s;
}

function HexToHexString(ByteBuffers)
{
    var AnArray = new Uint8Array(ByteBuffers);
    var Result = "";

    for(var i = 0; i < AnArray.length; i++)
    {
        if(i%2==0)
            Result += ApplyPadding(AnArray[i].toString(16), 2);
    }

    return Result;
}

function HexStringToHex(aString)
{
    var Buffer = new ArrayBuffer(aString.length*2); // 2 bytes for each char
    var BufferView = new Uint16Array(Buffer);

    for (var i = 0;i < aString.length; i++)
    {
        BufferView[i] = aString.charCodeAt(i);
    }
    return Buffer;
}

function OnReadFileCompletion(FileReadEvent)
{
    if(FileReadEvent.target.readyState == FileReader.DONE)
    {
        //document.getElementById('byte_content').textContent =FileReadEvent.target.result;
        var DataOfFile = HexStringToHex(FileReadEvent.target.result);
        var FinalData = HexToHexString(DataOfFile);
        document.getElementById('byte_content').textContent = FinalData;
        //FileReadEvent.target.result;
    }
}

function ReadFile(File, ResultFunction)
{
    var Reader = new FileReader();

    Reader.onloadend = ResultFunction;

    Reader.readAsBinaryString(File.slice(0, File.size - 1));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM