Node/Multer Get Filename

前端 未结 6 1831
甜味超标
甜味超标 2021-02-03 21:45

I am using the following to upload files to a directory via Multer. It works great, but I need to perform some actions after upload that require the name of the file I just post

6条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-03 22:30

        app.post('/multer', upload.single('file'), function(req, res) {
      // Need full filename created here
    const file = req.file
    if (!file) {
        const error = new Error('Please upload a file')
        error.httpStatusCode = 400
        return next(error)
      }
     res.send(file) #Here
    });
    

    You need recover file from this line

     res.send(file)
    

    using file.filename This output sample

    {
      "fieldname": "myFile",
      "originalname": "isc te esta esperando.jpg",
      "encoding": "7bit",
      "mimetype": "image/jpeg",
      "destination": "uploads",
      "filename": "myFile-1602293858948.eaf",
      "path": "uploads/myFile-1602293858948.eaf",
      "size": 297720
    } 
    

提交回复
热议问题