How to store a file with file extension with multer?

user3355603 picture user3355603 · Jul 23, 2015 · Viewed 51.7k times · Source

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?

Answer

sree picture sree · Sep 23, 2016

I have a workaround for the adding proper extension of files. If you use path node module

var multer = require('multer');
var path = require('path')

var storage = multer.diskStorage({
  destination: function (req, file, cb) {
    cb(null, 'uploads/')
  },
  filename: function (req, file, cb) {
    cb(null, Date.now() + path.extname(file.originalname)) //Appending extension
  }
})

var upload = multer({ storage: storage });