Node/Multer Get Filename

前端 未结 6 1832
甜味超标
甜味超标 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:25

    request.file gives the following stats, from which you would just need to pick request.file.originalname or request.file.filename to get the new filename created by nodejs app.

    { 
      fieldname: 'songUpload',
      originalname: '04. Stairway To Heaven - Led Zeppelin.mp3',
      encoding: '7bit',
      mimetype: 'audio/mp3',
      destination: './uploads',
      filename: 'songUpload-1476677312011',
      path: 'uploads/songUpload-1476677312011',
      size: 14058414 
    }
    

    Eg, in nodejs express mvc app with ecma-6,

    var Express = require('express');
    var app = Express();
    
    var multipartUpload = Multer({storage: Multer.diskStorage({
        destination: function (req, file, callback) { callback(null, './uploads');},
        filename: function (req, file, callback) { callback(null, file.fieldname + '-' + Date.now());}})
    }).single('songUpload');
    
    app.post('/artists', multipartUpload, (req, resp) => {
         val originalFileName = req.file.originalname
         console.log(originalFileName)
    }
    

提交回复
热议问题