✅ End point can't take byte[] from HTTP Request. Only int[].
If I use byte[] type, it gives me null.
30 Replies
The endpoint on the server?
Normally u dont combine formdata and json
How else am I gonna do?
json can't serialize byte[]
you either base64 the bytes, if you wanna keep it json, or you use HTTP file transfer
Former, I want to use the former.
okay, then just base64 the byte array into a string, then reverse that in the client
That went over my head.
set your models datatype to "string"
Then turn it into byte array?
in back end
wait, in what direction are you doing this? are you sending data from client to backend, or from backend to client?
client to backend lol
I am using fetch
ok that should still work fine
atob() - Web APIs | MDN
The atob() function decodes a
string of data which has been encoded using Base64 encoding. You can use
the btoa() method to encode and transmit
data which may otherwise cause communication problems, then transmit it and use the
atob() method to decode the data again. For example, you can encode,
transmit, and decode control characters ...
use this to turn your byte array into a string
err, maybe btoa
yeah, btoa => string
Argument of type 'Number[] | null' is not assignable to parameter of type 'string'.
Type 'null' is not assignable to type 'string'.
It wants a string.
You're having TS problems at this point, but at least show the code in question
cant very well guess what the hell you did
It's the await ImageToByteArray(file), everything is up there in my first comment.
I did something like
const asdf = btoa(await ImageToByteArray(file));
https://stackoverflow.com/questions/9267899/arraybuffer-to-base64-encoded-string ?
var base64String = btoa(String.fromCharCode.apply(null, new Uint8Array(arrayBuffer)));
or const base64String = btoa(String.fromCharCode(...new Uint8Array(arrayBuffer)));
this works fine.
obviously, you'd do something with data
(which is a byte[]) instead of just printing the bytes 🙂
Successfully turned the array into string
It's super duper long tho 😄
the string it gave me i mean
well yes
representing an image as a string isnt very space efficient
would it have problem with traffic? Costs more?
Let me do the reverse too in the meantime
byte[] Convert.FromBase64String(string input)
is all you need on the server sidewhat about joined
that was just something I had to "debug" it
ie, print the raw bytes
I see
you probably don't want to do that :p
Let's try this bad boy now
Heh heee boooii
"Internet guy", really? 😛
Thank you, kind internet guy
Let's close this, once again thank you. /close