Home > Software design >  how to send multiple images with multer and upload to cloudinary
how to send multiple images with multer and upload to cloudinary

Time:11-02

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  type="file" id="eventimage1" >
<input  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

CodePudding user response:

Danny from Cloudinary Support here.

Our Upload API doesn't currently support receiving multiple files at once. Instead, as we have quite a high concurrency rate, we would recommend using multiple threads to send many files at once.

You can also use asynchronous calls, and tell Cloudinary to do the upload in the background by adding the async parameter and setting it to true.

I hope this helps. If you wanted to discuss this further, I'd recommend opening a support ticket with us directly, and we'll be happy to assist :)

CodePudding user response:

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.

  • Related