在帖子中上传文件时,req.body在节点JS中是{}
react Code -fileUploader.js
// React - FileUplader.js
const handleSubmission = (e) => {
e.preventDefault();
if(isSelected === false){
alert("load the file");
}
else{
const formData = new FormData();
formData.append("certificate",selectFile);
// API CALL
fetch("http://localhost:8080/upload", {
method: "POST",
body: formData,
headers : {
"Content-Type" : "multipart/form-data"
}
}).then((response) =>response.json())
.then((result)=>{
console.log("Success : ", result);
})
.catch((error)=>{
console.error("Error : ",error);
});
}
};
nodejs code -server.js
app.use(cors());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended : true}));
app.post('/upload', async function(req ,res){
try {
const file = req.files; // undeifined
const bodyData = req.body; // {}
console.log("file : ",file);
console.log("bodyData : ",bodyData);
res.status(200).send({
message: "FILE RECEIVED!"
});
} catch(error){
res.send("ERROR")};
});
问题
为什么req.body在节点js中{}
我尝试使用multer,但得到了相同的结果
mdn表示formdata对象不是一个简单的对象,而是为xmlhttprequest创建的特殊对象传输,无法使用控制台记录。
React code - FileUploader.js
// React - FileUplader.js
const handleSubmission = (e) => {
e.preventDefault();
if(isSelected === false){
alert("load the file");
}
else{
const formData = new FormData();
formData.append("certificate",selectFile);
// API CALL
fetch("http://localhost:8080/upload", {
method: "POST",
body: formData,
headers : {
"Content-Type" : "multipart/form-data"
}
}).then((response) =>response.json())
.then((result)=>{
console.log("Success : ", result);
})
.catch((error)=>{
console.error("Error : ",error);
});
}
};
Nodejs code - Server.js
app.use(cors());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended : true}));
app.post('/upload', async function(req ,res){
try {
const file = req.files; // undeifined
const bodyData = req.body; // {}
console.log("file : ",file);
console.log("bodyData : ",bodyData);
res.status(200).send({
message: "FILE RECEIVED!"
});
} catch(error){
res.send("ERROR")};
});
Problem
Why req.body is {} in node js
I tried using MULTER but got the same result
MDN says that FormData object is not a simple object, but a special object created for XMLHttpRequest transmission and cannot be recorded with the console.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的前端代码正确。您没有设置Multer。 Multer实际上是中间件,将听
Multipart/form-data
。 [检查文档] [1]要在Node.js环境上处理文件,请编写一个中间件:
中使用它
然后在
Express App
app.use(BodyParser.urlencer.urlencoded({Extended:true})。 [1]: http://expressjs.com/en/resources/middleware/multer.html#:~: text=multer%20IS%20A%20A%20Node.smultipart%2fform%2fform%2fform%2ddata%20%20 )。
Your front end code is correct. You did not set up multer. Multer is actually a middleware, will be listening to
multipart/form-data
. [check the docs][1]to handle files on node.js environment, write a middleware:
Then use it in
express app
app.use(bodyParser.urlencoded({extended : true}));
[1]: http://expressjs.com/en/resources/middleware/multer.html#:~:text=Multer%20is%20a%20node.,multipart%2Fform%2Ddata%20).