在 javascript ArrayBuffer 中访问 Uint8Array
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/49885795/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Access Uint8Array in javascript ArrayBuffer
提问by pvitt
I've got a javascript ArrayBuffer generated from a FileReader ReadAsArrayBuffer method on a jpeg file.
我有一个从 jpeg 文件上的 FileReader ReadAsArrayBuffer 方法生成的 javascript ArrayBuffer。
I'm trying to access the UInt32 array of the ArrayBuffer and send to a WCF service (ultimately to be inserted into a database on the server).
我正在尝试访问 ArrayBuffer 的 UInt32 数组并发送到 WCF 服务(最终插入到服务器上的数据库中)。
I've seen an example here on stackoverflow (byte array method) where a UnInt32 array is converted to a byte array which I think would work.
我在这里看到了一个关于 stackoverflow(字节数组方法)的例子,其中一个 UnInt32 数组被转换为一个我认为可以工作的字节数组。
I'm trying to access the [[Uint8Array]] of my arrayBuffer variable below so I can send it to the WCF, but I'm not having much luck. I've tried:
我正在尝试访问下面我的 arrayBuffer 变量的 [[Uint8Array]] 以便我可以将它发送到 WCF,但我运气不佳。我试过了:
var arrayBuffer = reader.result[[Uint8Array]];//nope
var arrayBuffer = reader.result[Uint8Array];//nope
var arrayBuffer = reader.result.Uint8Array;//nope
var arrayBuffer = reader.result[1];//nope
Any ideas on how to access that [[Uint8Array]] would be appreciated. When the entire ArrayBuffer is sent to WCF Service I get a 0 byte array -- cant read it
关于如何访问 [[Uint8Array]] 的任何想法将不胜感激。当整个 ArrayBuffer 被发送到 WCF 服务时,我得到一个 0 字节的数组——无法读取它
Thanks
谢谢
Pete
皮特
回答by Patrick Evans
Those properties do not actually exist on the ArrayBuffer object. They are put there by the Dev Tools window for viewing the ArrayBuffer contents.
这些属性实际上并不存在于 ArrayBuffer 对象上。它们位于 Dev Tools 窗口中,用于查看 ArrayBuffer 内容。
You need to actually create the TypedArray of your choice through its constructor syntax
您需要通过其构造函数语法实际创建您选择的 TypedArray
new TypedArray(buffer [, byteOffset [, length]]);
new TypedArray(buffer [, byteOffset [, length]]);
So in your case if you want Uint8Arrayyou would need to do:
因此,在您的情况下,如果您愿意,Uint8Array您需要执行以下操作:
var uint8View = new Uint8Array(arrayBuffer);


