Node.js无法读取XHR2 FormData数据

时间:2013-03-08 13:57:06

标签: node.js file-upload xmlhttprequest form-data

在客户端我使用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信息,它们都是空对象

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

1 个答案:

答案 0 :(得分:0)

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