Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
178 views
in Technique[技术] by (71.8m points)

node.js - How to create a filename from frontend?

I am creating a React and Node application, and at the moment I need to upload a PDF file, the code works well, but I was wondering how to use front variables to create the file name by multer.

The code...

Front:

const handleSubmit = async (e) => {
      const dado = new FormData() 
      const year = data.split("/")
      const dd = year[0];
      const mm = year[1];
      const aaaa = year[2];
      dado.append('file', file)
      dado.append('uni', uni)
      dado.append('dd', dd)
      dado.append('mm', mm)
      dado.append('aaaa', aaaa)
      console.log(dado)
      axios.post("http://localhost:8080/api/comprebem/file", dado, { 
    })
    .then(res => { 
      console.log(res.statusText)
    })
}

Back:

const storage = multer.diskStorage({
  destination:"../login/public/comprebem/ibama/",
  filename: function (req, file, cb) { 
    console.log(file);
    cb(null, `unidade${"req['uni']"}_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)

  })

});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...