Multer: how to name files after req.body parameters
I am trying to upload a file with a form like below
<input type="file" name="collateral" />
<input type="hidden" name="id" value="ABCDEFG" />
<input type="submit" value="Upload Image" name="submit">
and I would like to rename the file to the input name id (ABCDEFG). Since I cannot access the req.body through the rename: (filename, filename) function, I was wondering how I would do this?
+3
source to share
1 answer
Try putting the file last in your POST request payload.
Then you can access req.body
via this callback:
var multer = require('multer');
var storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, './public/uploads/')
},
filename: function (req, file, cb) {
cb(null, file.fieldname + '-' + Date.now())
// access req.body and rename file
}
});
var upload = multer({ storage: storage });
0
source to share