user3355603 user3355603 - 1 month ago 12
Node.js Question

How to store a file with file extension with multer?

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?


From the docs: "Multer will not append any file extension for you, your function should return a filename complete with an file extension."

Here's how you can add the extension:

var multer = require('multer');

var storage = multer.diskStorage({
  destination: function (req, file, cb) {
    cb(null, 'uploads/')
  filename: function (req, file, cb) {
    cb(null, + '.jpg') //Appending .jpg

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

I would recommend using the mimetype property to determine the extension. For example:

filename: function (req, file, cb) {
  console.log(file.mimetype); //Will return something like: image/jpeg

More info: