【发布时间】:2013-02-24 02:12:27
【问题描述】:
在客户端我使用 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.files和req.body的信息,都是空对象
那么如何获取上传文件的信息,例如保存路径、大小或名称?
【问题讨论】:
标签: node.js file-upload xmlhttprequest form-data