✅ End point can't take byte[] from HTTP Request. Only int[].

LLary2/15/2023
//model
public int[]? coverImage { get; set; }

//In a function
var coverimg_file = coverImg_input.files;
if (coverimg_file && coverimg_file[0]) {
     var file = coverimg_file[0];
     formData.coverImage = await ImageToByteArray(file);
     //coverImage!: Number[] | null;
     //ArrayBuffer type didn't work, idk i didn't dig in too much
}

async function ImageToByteArray(file: File): Promise<Number[] | null> {
    return new Promise((resolve) => {
        const reader = new FileReader()
        reader.onloadend = () => {
            var data = reader.result as ArrayBuffer | null;
            if (data != null) {
                return resolve([...new Uint8Array(data)]);
            }
            else {
                return resolve(null);
            }
        };
        reader.readAsArrayBuffer(file);
    })
}

//Fetch init;
{
    method: 'POST',
    headers: {
       'Accept': 'application/json',
       'Content-Type': 'application/json'
    },
    body: JSON.stringify(formData)
}

If I use byte[] type, it gives me null.
DDusty2/15/2023
The endpoint on the server?
DDusty2/15/2023
Normally u dont combine formdata and json
LLary2/15/2023
How else am I gonna do?
PPobiega2/15/2023
json can't serialize byte[]
PPobiega2/15/2023
you either base64 the bytes, if you wanna keep it json, or you use HTTP file transfer
LLary2/15/2023
Former, I want to use the former.
PPobiega2/15/2023
okay, then just base64 the byte array into a string, then reverse that in the client
LLary2/15/2023
That went over my head.
PPobiega2/15/2023
set your models datatype to "string"
LLary2/15/2023
Then turn it into byte array?
LLary2/15/2023
in back end
PPobiega2/15/2023
wait, in what direction are you doing this? are you sending data from client to backend, or from backend to client?
LLary2/15/2023
client to backend lol
LLary2/15/2023
I am using fetch
PPobiega2/15/2023
ok that should still work fine
PPobiega2/15/2023
use this to turn your byte array into a string
PPobiega2/15/2023
err, maybe btoa
PPobiega2/15/2023
yeah, btoa => string
LLary2/15/2023
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.
PPobiega2/15/2023
You're having TS problems at this point, but at least show the code in question
PPobiega2/15/2023
cant very well guess what the hell you did
LLary2/15/2023
It's the await ImageToByteArray(file), everything is up there in my first comment.

I did something like const asdf = btoa(await ImageToByteArray(file));
PPobiega2/15/2023
var base64String = btoa(String.fromCharCode.apply(null, new Uint8Array(arrayBuffer)));
PPobiega2/15/2023
or const base64String = btoa(String.fromCharCode(...new Uint8Array(arrayBuffer)));
PPobiega2/15/2023
[HttpPost]
public IActionResult Test(ByteArrayModel model)
{
    var data = Convert.FromBase64String(model.Content);
    var joined = string.Join(", ", data.Select(x => Convert.ToString(x)).ToArray());

    return Ok(joined);
}
PPobiega2/15/2023
this works fine.
PPobiega2/15/2023
obviously, you'd do something with data (which is a byte[]) instead of just printing the bytes 🙂
LLary2/15/2023
async function ArrayBufferToBase64(file: File): Promise<string | null> {
    return new Promise((resolve) => {
        const reader = new FileReader()
        reader.onloadend = () => {
            var buffer = reader.result as ArrayBuffer | null;
            if (buffer != null) {
                var binary = '';
                var bytes = new Uint8Array(buffer);
                var len = bytes.byteLength;
                for (var i = 0; i < len; i++) {
                    binary += String.fromCharCode(bytes[i]);
                }
                return resolve(window.btoa(binary));
            }
            else {
                return resolve(null);
            }
        };
        reader.readAsArrayBuffer(file);
    })
}

Successfully turned the array into string
LLary2/15/2023
It's super duper long tho 😄
LLary2/15/2023
the string it gave me i mean
PPobiega2/15/2023
well yes
PPobiega2/15/2023
representing an image as a string isnt very space efficient
LLary2/15/2023
would it have problem with traffic? Costs more?
LLary2/15/2023
Let me do the reverse too in the meantime
PPobiega2/15/2023
byte[] Convert.FromBase64String(string input)
PPobiega2/15/2023
is all you need on the server side
LLary2/15/2023
what about joined
PPobiega2/15/2023
that was just something I had to "debug" it
PPobiega2/15/2023
ie, print the raw bytes
LLary2/15/2023
I see
PPobiega2/15/2023
you probably don't want to do that :p
LLary2/15/2023
Let's try this bad boy now
LLary2/15/2023
Heh heee boooii
Image
PPobiega2/15/2023
"Internet guy", really? 😛
LLary2/15/2023
Thank you, kind internet guy
LLary2/15/2023
Let's close this, once again thank you. /close