Search code examples
javascriptnode.jsmultercloudinary

how to send multiple images with multer and upload to cloudinary


I'm trying to send multiple images to nodejs and upload them to Cloudinary using multer, not multiple images from one file input, but from different file inputs, like so:

<input class="form-control" type="file" id="eventimage1" >
<input class="form-control" type="file" id="eventimage2" >

but I don't know if multer can do it. There are some solutions online but it doesn't apply to may case, I don't know if there is a way to manipulate this:

var upload = multer();
app.post('/PATH', upload.array('uploadedImages', 10),FUNCTION)

to solve the issue multer has.

this is what I'm currently working with:

var upload = multer();
app.post('/PATH', upload.single("Upload_event_image"),FUNCTION)

and sending it to Cloudinary this way:

const img = await cloudinary.uploader.upload(req.file.path, {
      public_id: `${Event_title}_eventImage${Event_owners_id}`,
    });

so my question is how can I get the multiple images with multer and send all of them using Cloudinary


Solution

  • Use upload.any() and create one upload request for each entry in req.files:

    app.post("/PATH", upload.any(), async function(req, res) {
      await Promise.all(
        req.files.map(file => cloudinary.uploader.upload(file.path, ...))
      );
      res.send(???);
    });
    

    The question is what you want in the response to that request.