【问题标题】:Node.js can't read XHR2 FormData dataNode.js 无法读取 XHR2 FormData 数据
【发布时间】: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.filesreq.body的信息,都是空对象

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

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2012-02-01
      • 1970-01-01
      • 2017-05-01
      • 2017-11-12
      • 2015-03-14
      • 1970-01-01
      • 2020-08-25
      • 2015-08-01
      • 1970-01-01
      相关资源
      最近更新 更多