How to modify the multer filename with front variables?

Asked

Viewed 53 times

0

Good morning, I am creating an application React and Node, and at the moment I need to upload a PDF file, the code works well, but I wanted to know how when it comes to mounting the filename of the file by the multer, I use variables of the front itself? thank you.

The code is as follows:

Front:

const sendFile = () => {
      const dado = new FormData() 
      dado.append('file', file)
      axios.post("http://localhost:8080/api/comprebem/file", dado, { // receive two parameter endpoint url ,form data 
    })
    .then(res => { // then print response status
      console.log(res.statusText)
    })
    }

Back:

const storage = multer.diskStorage({
  destination:"../login/public/comprebem/ibama/",
  filename: function (req, file, cb) {
    cb(null, `unidade${Date.now()}_data${file.originalname}` )
}
})

const upload = multer({ storage: storage }).single('file')

app.post('/api/comprebem/file',function(req, res) {
     
  upload(req, res, function (err) {
         if (err instanceof multer.MulterError) {
             return res.status(500).json(err)
         } else if (err) {
             return res.status(500).json(err)
         }
    return res.status(200).send(req.file)

  })

});

If anyone can answer me too why you’re making that mistake:

inserir a descrição da imagem aqui

It gives when I send the file, but it is working all correctly, why there is error?

  • como que na hora de montar o filename do arquivo pelo multer, eu uso variáveis do próprio front?, could try to extract these variables by req in filename: function (req, file, cb) {...}? Tried to do that?

  • I try to use req.var, req[var], req['var'], but nothing works, returns Undefined

  • req.body.var?

  • keeps giving indefinite, on the front I’m giving append in the State that contains the variable q I want to take back, I’m doing wrong? dado.append('file', file)
 dado.append('uni', uni)

No answers

Browser other questions tagged

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