Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access Uint8Array in javascript ArrayBuffer

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

like image 238
pvitt Avatar asked Apr 17 '18 19:04

pvitt


People also ask

Is Uint8Array an ArrayBuffer?

For instance: Uint8Array – treats each byte in ArrayBuffer as a separate number, with possible values from 0 to 255 (a byte is 8-bit, so it can hold only that much). Such value is called a “8-bit unsigned integer”. Uint16Array – treats every 2 bytes as an integer, with possible values from 0 to 65535.

What is ArrayBuffer in JavaScript?

The ArrayBuffer object is used to represent a generic, fixed-length raw binary data buffer. It is an array of bytes, often referred to in other languages as a "byte array".

Is ArrayBuffer same as buffer?

1. A Buffer is just a view for looking into an ArrayBuffer . A Buffer , in fact, is a FastBuffer , which extends (inherits from) Uint8Array , which is an octet-unit view (“partial accessor”) of the actual memory, an ArrayBuffer .

How do you concatenate Uint8Array?

You can use the set method. Create a new typed array with all the sizes. Example: var arrayOne = new Uint8Array([2,4,8]); var arrayTwo = new Uint8Array([16,32,64]); var mergedArray = new Uint8Array(arrayOne.


1 Answers

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);
like image 163
Patrick Evans Avatar answered Oct 11 '22 21:10

Patrick Evans