Access Uint8Array in javascript ArrayBuffer

pvitt picture pvitt · Apr 17, 2018 · Viewed 17.1k times · Source

I've got a javascript ArrayBuffer generated from a FileReader ReadAsArrayBuffer method on a jpeg file.

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).

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.

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:

   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

Thanks

Pete

enter image description here

Answer

Patrick Evans picture Patrick Evans · Apr 17, 2018

Those properties do not actually exist on the ArrayBuffer object. They are put there by the Dev Tools window for viewing the ArrayBuffer contents.

You need to actually create the TypedArray of your choice through its constructor syntax

new TypedArray(buffer [, byteOffset [, length]]);

So in your case if you want Uint8Array you would need to do:

var uint8View = new Uint8Array(arrayBuffer);