How to send an ArrayBuffer as binary in a multipart/form-data request using JavaScript?

I’m trying to send a file (such as a PNG image) as binary data using a multipart/form-data request in JavaScript. I want to ensure that the file’s binary data is sent correctly without converting it to characters (which corrupts the binary data).

Currently, I have a File object that I convert to an ArrayBuffer:

const fileContent = await fileData.file.arrayBuffer();

I want to append this binary data to the request body in a multipart/form-data format. However, I’m not sure how to correctly iterate over the ArrayBuffer and append the binary data as-is.

Here’s a simplified version of what I’m doing:

// Now, I want to append the binary content to the body
const uint8Array = new Uint8Array(fileContent);

for (let i = 0; i < uint8Array.length; i++) {
  body += String.fromCharCode(uint8Array[i]); // This corrupts binary data
}

// Then I send `body` in a POST request


This works for text files, but it corrupts the png, pdf…

I tried everything. I know there are other ways of sending a request, but i had other problems when trying it like that. So i am basically stuck with this one.