send arraybuffer from angularjs to c# endpoint - javascript

I have an ArrayBuffer in angularjs that I am trying to send to my backend endpoint in c#, I thought it was a regular byte array but it's not mapping appropriately
What would an ArrayBuffer map to as an object in c# if not a byte[]?.
I have a PDF file represented as that ArrayBuffer but now I would like to send it to the server, or perhaps is there a way to convert that ArrayBuffer to a File type in JavaScript and send it o the endpoint so that I can use something like an IFormFile?.
Thank you

I had a situation of same some long days back. What I did was, I converted the array buffer data to base 64 string in the js side and sent it to the backend c# code using post method,and again I decoded it to array buffer to regenerate the file on server side. By this way I achieved the mapping.
I used this code which helps to convert the array buffer to base 64 string in JS.
and this api of .net framework to convert back the base 64 string to file buffer.
I hope this would help you.

Related

How to convert image to byte array using js

I am trying to convert image to byte array using only javascript and send to api. I have found this answer but it only shows to convert to base64 then send it to server side. Are there any other ways to convert to byte array using js or I can't just send byte array to api ?

How to transfer aes key from js to java server?

I am trying to transfer AES key generate in js:
var AESkey = forge.random.getBytesSync(16);
exapmle if printed condsole i have "§½­üå8bdÈP"
but printed in my java server it is "—§‡½­\u001eüå8b\u000ed�\u0012P›"
(other data are ok)
I tried to get the bytes with Buffer.from(AESkey) but instead of getting a 16 bytes buffer I get a 22/24/25.. buffer lenghts.
In which format can I transfer the key and how can I get my AESkey to that format in js?
A possible solution:
make a base64 encoded string on client side
send base64-encoded string to server
decode base64 in Java to get the byte sequence back
This way the byte sequence can be safely transported.

Jersey client side receive pojo us bytes

Is there any possibility to have a Restful method (Java EE) that returns back to the client a POJO serialized us bytes (using ByteArrayOutputStream, FlatBuffers or some other library)?
And the client to cast the bytes back to a POJO object by using jquery or javascript in general (us client side)?
What I had found until now, is receiving back a JSON/XML.
Directly converting POJO to bytes won't work as your client (javascript or so) won't know how to convert the bytes back to POJO. What you can do is this. Say your response is objA. The do this. Convert objA to String (JSON maybe) and then Base64 encode it to get byte[]. Put it inside a wrapper object objB. Return objB as a JSON from your service. Your client can just take that byte[] and since it knows that the byte[] is base64 it will be able to get the original information from there.
May I ask the intent of doing this instead of XML/JSON?

Send and receive a blob in javascript

I want to send a blob using a JQuery ajax request and receive it server-side with Node.js + express.
I would send the blob as a JSON string, but it seems that none of the binary data is included in it:
{"type":"audio/wav","size":344108}
How else could it be sent?
JSON doesn't support binary data. You will have to encode your binary data first: Binary Data in JSON String. Something better than Base64

How would I send and receive packets over a WebSocket in Javascript

I want to send data from Javascript to a WebSocket server and also from a WebSocket server to Javascript.
I want to send this:
Headers
-------
Field 1: 2 byte hex
Field 2: 2 byte hex
Field 3: 4 byte hex
Data
----
Field1 : 2 byte hex
Field1 : 8 byte hex
From Javascript, I can send a two-byte value via
socket = new WebSocket(host);
...
socket.send(0xEF);
But I want to send multiple fields, together...let's say 0xEF, 0x60, and 0x0042.
How do I do this?
And, how to I interpret via Javascript data containing multiple fields coming from the WebSocket server?
You can send data as a string. For example:
socket.send("hello world");
I recommend you to use JSON as data format. You can convert JSON strings directly into objects and vice versa. It's so simple and useful!
You can send data as JSON objects.
socket.send(JSON.stringify({field1:'0xEF', field2:'0x60',field3: '0x0042'}));
Sound like what you are asking is how to send binary data over a WebSocket connection.
This is largely answered here:
Send and receive binary data over web sockets in Javascript?
A bit of extra info not covered in that answer:
The current WebSocket protocol and API only permits strings to be sent (or anything that can be coerced/type-cast to a string) and received messages are strings. The next iteration of the protocol (HyBi-07) supports binary data is currently being implemented in browsers.
Javascript strings are UTF-16 which is 2 bytes for every character internally. The current WebSockets payload is limited to UTF-8. In UTF-8 character values below 128 take 1 byte to encode. Values 128 and above take 2 or more bytes to encode. When you send a Javascript string, it gets converted from UTF-16 to UTF-8. To send and receive binary data (until the protocol and API natively support it), you need to encode your data into something compatible with UTF-8. For example, base64. This is covered in more detail in the answer linked above.

Categories