50

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

1 Answer 1

90

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);
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks. Now I've got to get the WCF service to read/accept the UIntArray
getting error Argument of type 'string | ArrayBuffer' is not assignable to parameter of type 'ArrayBuffer | ArrayLike<number> | SharedArrayBuffer'. Type 'string' is not assignable to type 'ArrayBuffer | ArrayLike<number> | SharedArrayBuffer'.ts(2769)
@Sreeraj_ms you need to specify arrayBuffer 's type: var uint8View = new Uint8Array(arrayBuffer as ArrayBuffer);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.