How to store a file with file extension with multer?

后端 未结 11 1605
情话喂你
情话喂你 2020-12-13 17:56

Managed to store my files in a folder but they store without the file extension.

Does any one know how would I store the file with file extension?

11条回答
  •  攒了一身酷
    2020-12-13 18:21

    I am doing like this

    var multer  = require('multer');
    
    var storage = multer.diskStorage({
      destination: function (req, file, cb) {
        cb(null, './public/uploads/img/')
      },
      filename: function (req, file, cb) {
        let ext = file.originalname.substring(file.originalname.lastIndexOf('.'), file.originalname.length);
        cb(null, Date.now() + ext);
      }
    })
    
    var upload = multer({ storage: storage }).single('eventimage');
    

提交回复
热议问题