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
});
using request.file.filename
fieldname Field name specified in the form
originalname Name of the file on the user's computer encoding Encoding type of the file
mimetype Mime type of the file
size Size of the file in bytes
request.file
gives the following stats, from which you would just need to pickrequest.file.originalname
orrequest.file.filename
to get the new filename created by nodejs app.Eg, in nodejs express mvc app with ecma-6,
Accessing uploaded files data differs in Multer, depending whether you are uploading single or multiple files. Access data like so:
uploading single file:
uploading multiple files: