Problems with formData in Multiple Upload Express and Multer

Asked

Viewed 48 times

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?

  • Tested yes, it generates the following error with this header "Error: Multipart: Boundary not found"

  • See if it helps. You tried to declare the directive boundary in this way: "Content-Type": "multipart/form-data; boundary=${tamanho_aqui}".

  • 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] {}"

  • A detail I forgot to mention.... this here data.append("images", file); writes about the field images with the latter file? Wouldn’t it be better to push an array arr.push(file) and then insert into the field images (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.

  • I drew, as soon as I get here I will do tests with the push and update here! Thanks!

Show 1 more comment
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.