brohxa
brohxa

Reputation: 119

Multer req.files is empty

I am trying to upload a array of image using Multer. In the client side I have the array which needs to be sent to the server. below I will attach the array screenshot.

As you can see in the below screenshot the array is present but in the server side its empty [ ].

In multer docs its written use req.files to get the desired array.

I also tried to console.log(req) to see where the data is actual going and I found out that its go to req.body! Its weird please help me out guys.

enter image description here

Client Side Code Below

    const formData = new FormData();
    formData.append("portfolio", portfolio);
    formData.append("vendorEmail", vendorEmail); 

Server Side Code Below


//MULTER INIT
const storage = multer.diskStorage({
  destination: function (req, file, cb) {
    cb(null, "/tmp");
  },
  filename: function (req, file, cb) {
    const uniqueSuffix = Date.now() + "-" + Math.round(Math.random() * 1e9);
    cb(null, file.fieldname + "-" + uniqueSuffix);
  },
});

const upload = multer({
  storage: storage,
});

router.post(
  "/portfolio-upload-bulk",
  upload.array("portfolio"),
  (req, res, next) => {
    const imgObj = JSON.parse(JSON.stringify(req.files));

    console.log(req.files);
  }
);

In Console its empty

enter image description here

Upvotes: 0

Views: 1256

Answers (1)

brohxa
brohxa

Reputation: 119

I got the solution. Instead of directly appending like this:

formData.append("portoflio", portfolio);

I did this:

for (let i = 0; i < portfolio.length; i++) {
  formData.append("portfolio", portfolio[i]);
}

Upvotes: 1

Related Questions