Node/Multer Get Filename

Kode picture Kode · Dec 17, 2015 · Viewed 40.5k times · Source

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 posted to the "upload" directory. How do I get the name of the file I just posted?

// Multer storage options
var storage = multer.diskStorage({
  destination: function(req, file, cb) {
    cb(null, 'upload/');
  },
  filename: function(req, file, cb) {
    cb(null, file.originalname + '-' + Date.now() + '.pdf');
  }
});

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

app.post('/multer', upload.single('file'), function(req, res) {
  // Need full filename created here
});

Answer

Yash Bele picture Yash Bele · Apr 10, 2017
var express=require("express");
var app=express();
var multer=require("multer");
var upload=multer({dest:"uploads/"});
app.post("/multer", upload.single("file"), function(req,res){
    console.log(req.file.filename);
});