Open ChaseWindYoungs opened 1 year ago
by the way, is chatGPT told me to use body-parser
You will need to use req.query passing query parameters from browser context
//in Browser Context
async function uploadFile(file, fileType, index) {
const fileName = `${fileType}${index}`;
const formData = new FormData();
formData.append('file', file);
const response = await fetch(`/api/upload?filename=${encodeURIComponent(fileName)}`, { method: 'POST', body: formData });
if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`);
return response.json();
}
//in NodeJS
const uploadFiles = async (req, res) => {
console.log(req.query.filename)
const upload = multer({
storage: multer.diskStorage({
destination: 'uploads/',
filename: (req, file, cb) => cb(null, `${req.query.filename}`)
})
});
upload.single('file')(req, res, (err) => {
if (err) return res.send(err);
console.log(`Received file ${req.query.filename}`);
res.json({ status: 'OK', filename: req.file.originalname });
});
}
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
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