我无法将图像和文本从React一起上传到我的Node.js后端。我可以分别上传两个文件,但是我正努力将它们一起上传。这是我到目前为止的内容:
反应:
postData只是文本字段(例如{title:'hello}'
)
const formData = new FormData();
formData.append("image", postData.image);
delete postData.image;
axios({
method: "post",
url: "/api/post",
data: { formData, postData }
})
NodeJ: 尝试接收req.body中的postData和req.file中的formData
router.post(
"/",
passport.authenticate("jwt", { session: false }),
(req, res) => {
let fileLoc;
// const { errors, isValid } = validatePostInput(req.body);
console.log(req.body);
upload(req, res, err => {
console.log(req.file);
if (err) {
return res.status(200).json({ error: err });
} else if(req.file !== undefined) {
fileLoc = req.file.location;
}
});
}
);
感谢您的帮助