C#C
C#3y ago
49 replies
İrşat

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

//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.
Was this page helpful?
✅ End point can't take byte[] from HTTP Request. Only int[]. - C#