在客户端我使用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
信息,它们都是空对象
那么如何获取上传文件的信息,如保存路径,大小或名称?
答案 0 :(得分:0)
我尝试删除“defer:true”配置,它对我有用。