expressjs / expressjs/multer

How can I get other form keys in req.body before multer processing file?

Open
#1,201 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
12.1k
Forks
1.1k
Avg merge
8d 2h
Merged PRs (30d)
21

Description

First, I want use multer in a single router, and this request‘s data not only a file, but also include other form data,
these are part of important code
```
const bodyParser = require("body-parser");
const staticPath = path.join(__dirname, "..", "static");
const urlencodedParser = bodyParser.urlencoded({ extended: false }); // create application/json parser

const multerStorage = multer.diskStorage({
destination: function (req, file, cb) {
try {
if (!fs.existsSync(path.join(staticPath, "cache"))) {
fs.mkdirSync(path.join(staticPath, "cache"));
}
cb(null, path.join(staticPath, "cache"));
} catch (error) {
console.log("destination = error ======>", error);
}
},
filename: async (req, file, cb) => {
try {
console.log('req.body =============>', req.body.fileMD5)
console.log("file ====> ", file);
const { fieldname, originalname, mimetype } = file;
let fileFormat = originalname.split(".");
const uniqueSuffix = Date.now() + "-" + Math.round(Math.random() * 1e9);
cb(
null,
`${fieldname}-${uniqueSuffix}.${fileFormat[fileFormat.length - 1]}`
);
} catch (error) {
console.log("filename = error ======>", error);
}
},
});
const uploadCache = multer({ storage: multerStorage });

router.post("/sliceFile", urlencodedParser, uploadCache.single("file"), (req, res) => {
console.log('urlencodedParser =====> ',req.body);
try {
res.send({
code: 200,
message: "test",
});
} catch (error) {
console.log(error);
}
});

module.exports = router;
```
before uploadCache.single("file"), I want use urlencodedParser to processing form‘s fileMD5, used as a filename to save a file , but I can't get any in rer.body during multerStorage, can you tell me how to use form other data during multerStorage handle file

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.