【问题标题】:file upload from axios to busboy never finishes从 axios 上传到 busboy 的文件永远不会完成
【发布时间】:2018-09-17 17:38:45
【问题描述】:

这是我上传文件的简单代码。我在服务器上使用 nodejs 和 busboy,在客户端上使用 axios 和 react。我选择文件,提交表单,我只看到 /upload 在服务器端处理:

app.post('/upload', function (req, res) {
    console.log("upload"); // we do get here
    if (req.busboy) {
         // and here
        req.busboy.on("file", function (fieldName, fileStream, fileName, encoding, mimeType) {
            console.log("on file"); // but never get here
        });
        return req.pipe(req.busboy);
    }
});

但是永远不会调用 on("file") 回调。这是我的客户代码: 客户端代码:

<input type="file" onChange={this.getFile} />

getFile(e){
    e.preventDefault();
    let reader = new FileReader();
    let file = e.target.files[0];
    reader.onloadend = (theFile) => {
        const data = new FormData();
        data.append('file', file);
        axios.post("/upload",file);
    };
    reader.readAsDataURL(file);
}

可能是什么问题?

更新:req.body 未定义。我添加了以下几行:

var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({extended: false, limit: '50mb'}));

req.body 不为空且包含文件缓冲区,但 on('file') 尚未启动。

【问题讨论】:

  • 检查 req.body 并检查是否可以在其中找到文件缓冲区。
  • @mrdotb,req.body 未定义...
  • 如果 req.body 未定义,那么这是客户端问题。你可能想通了。

标签: node.js express axios busboy


【解决方案1】:

错误就在这里:

reader.onloadend = (theFile) => {
    const data = new FormData();
    data.append('file', file); 
    axios.post('/upload', data); // <-- I had wrong second argument here!
};

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多