Upload two files from two different Fields with multer

Asked

Viewed 84 times

2

Using the ladies I am trying to upload two different files that comes from Fields in forms different and has different storage path also:

let storageAudios = multer.diskStorage({
  destination: (req, file, callback) => {
    callback(null, './uploads/audiographs');
  },
  filename: (req, file, callback) => {
    callback(null, file.fieldname + '-' + Date.now() + path.extname(file.originalname));
  }
});

let uploadAudios = multer({
  storage: storageAudios
}).single('audiographs');



let storageEstablishments = multer.diskStorage({
  destination: (req, file, callback) => {
    callback(null, './uploads/establishments-logo');
  },
  filename: (req, file, callback) => {
    callback(null, file.fieldname + '-' + Date.now() + path.extname(file.originalname));
  }
});

let uploadEstablishments = multer({
  storage: storageEstablishments
}).single('logo');

In the controller of each route to make the uploads have:

addEstablishment: (req, res) => {
    uploadEstablishments(req, res, function(err) {
      if(err) {
        console.log(err)
        return res.end("error");
      }else{

        // ...

        console.log(req.file) // retornando undefined

        // ...
      }
    })
},
addAudio: (req, res) => {
  uploadAudios(req, res, function(err) {
    if(err) {
      console.log(err)
      return res.end("error");
    }else{

        // ...

        console.log(req.file) // retornando o arquivo esperado

        // ...
    }
  });
}

As can be seen, the upload audio works, but the establishments, no. I’m doing something wrong?

Thanks in advance for any help.

No answers

Browser other questions tagged

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