multer callbacks not working ?
It seems the usage has been changed over time. Currently, multer
constructor only accepts following options (https://www.npmjs.com/package/multer#multer-opts):
dest
orstorage
- Where to store the filesfileFilter
- Function to control which files are acceptedlimits
- Limits of the uploaded data
So, for example the renaming is to be solved by configuring appropriate storage (https://www.npmjs.com/package/multer#storage).
var storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, '/tmp/my-uploads'); // Absolute path. Folder must exist, will not be created for you.
},
filename: function (req, file, cb) {
cb(null, file.fieldname + '-' + Date.now());
}
})
var upload = multer({ storage: storage });
app.post('/profile', upload.single('fieldname'), function (req, res, next) {
// req.body contains the text fields
});
The fieldname
must match the field name in the request body. That is, in case of HTML form post, the form upload element input name.
Also have a look for other middleware functions like array
and fields
- https://www.npmjs.com/package/multer#single-fieldname which provide a a little different functionality.
Also you may be interested in the limits (https://www.npmjs.com/package/multer#limits) and file filter (https://www.npmjs.com/package/multer#filefilter)
And also - source is the single source of truth - have a peek!(https://github.com/expressjs/multer/blob/master/index.js)