3
I’m having trouble uploading multiple images to the backend. The method in the backend is correct, when I test via insominia the right one, however from my front it does not generate the data correctly.
As soon as I click on the images to upload I run the following function:
let data = new FormData();
for (const file of files) {
data.append("images", file);
}
data.append("clientId", "2");
const response = await requestAPI({
params: `/image/upload`,
method: "POST",
headers: {
'Content-Type' : undefined,
},
body: data,
});
And in my backend I hope this way :
Route :
this.router.post(
'/upload',
upload.array('images', 20),
(req: Request, res: Response) => this.imageController.imageUpload(req, res)
)
Both the req.files
how much req.body
return undefined
(empty) in controller.
Note: If I don’t pass the header as undefined
nothing happens, passing thus generates the following error:
images is not iterable
Hello Victor, I have some questions, first, you did not get to test with
"Content-Type": "multipart/form-data"
in the header?– Cmte Cardeal
Tested yes, it generates the following error with this header "Error: Multipart: Boundary not found"
– Victor Guedes
See if it helps. You tried to declare the directive
boundary
in this way:"Content-Type": "multipart/form-data; boundary=${tamanho_aqui}"
.– Cmte Cardeal
I tried to hold the same past on the link you sent and also had already tried other ways with Boundary to put anything successfully. With it the return turns " files = [] body : [Object null prototype] {}"
– Victor Guedes
A detail I forgot to mention.... this here
data.append("images", file);
writes about the fieldimages
with the latterfile
? Wouldn’t it be better to push an arrayarr.push(file)
and then insert into the fieldimages
(data.append("images", arr);
)? I’m trying to help, but it gets a little difficult without testing because I’m running out of time, but if I can do some tests, I can try to help even more.– Cmte Cardeal
I drew, as soon as I get here I will do tests with the push and update here! Thanks!
– Victor Guedes