How to programatically upload images/files using /upload endpoint?

System Information
  • Strapi Version: 3.6
  • Operating System: macOS Big Sur
  • Database: SQLite

I am trying to upload an image using the /upload endpoint, how this part of the documentation says: Upload - Strapi Developer Documentation, however, I keep getting this error: { "statusCode": 400, "error": "Bad Request", "message": "Bad Request", "data": { "errors": [ { "id": "Upload.status.empty", "message": "Files are empty" } ] } }

This is my code: `fs.readFile("./images/" + “image.jpg”, async (err, file: Buffer) => {
if (err) {
return err;
}

const data = {
files: file,
refId: 26,
ref: “athlete”,
field: “cover_photo”,
};
let formDataToSend = new FormData();
for (const key of Object.keys(data)) {
formDataToSend.append(key, data[key]);
}

try {
const res = await fetch(UPLOAD_URL, {
method: “POST”,
body: formDataToSend,
});
const json = await res.json();
console.log(JSON.stringify(json, null, 2));
} catch (e) {
console.error(JSON.stringify(e, null, 2));
}
});`

Can anyone please tell me what I’m doing wrong?