gpt4 book ai didi

Node.js 无法读取 XHR2 FormData 数据

转载 作者:搜寻专家 更新时间:2023-11-01 00:08:53 25 4
gpt4 key购买 nike

在客户端我使用 XHR2 Formdata 在 ajax 中上传文件:

var send = function (file) {
var xhr = new XMLHttpRequest();
xhr.file = file;
xhr.open('post', '/upload', true);
xhr.send(file);
}

var fd = new FormData();
fd.append('image', _file); // the _file is my file

xhr.send(fd);

在 node.js 服务器中:

app.configure(function(){
app.use(express.bodyParser({
uploadDir: "public/images",
keepExtensions: true,
limit: 10000000, /* 10M 10*1000*1000 */
defer: true
}));
app.use(express.static(__dirname + "/public"));
});



app.post("/upload", function (req, res) {
console.log(req.files);
console.log(req.body);

res.send("ok");
})

奇怪的是,文件可以上传成功,它无法记录req.filesreq.body信息,它们都是空对象

那么如何获取上传文件的信息,如保存路径、大小或名称?

最佳答案

我尝试删除“defer: true”配置,它对我有用。

关于Node.js 无法读取 XHR2 FormData 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15295596/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com