【问题标题】:Error "Unexpected end of multipart data" in busboy file uploadbusboy 文件上传中的错误“多部分数据意外结束”
【发布时间】:2017-10-09 21:09:10
【问题描述】:

我正在使用 connect-busboy 在 node/express 应用程序中上传文件。问题是有时它可以工作(文件成功上传),有时我收到错误 Unexpected end of multipart data 和应用程序崩溃。这可能是什么原因错误?也将不胜感激有关如何调试此问题的任何帮助。我正在使用node version 5connect-busboy": "0.2.14"提前谢谢你

router.route('/images')    
  .post (function(req, res) {

  var fstream;
  req.busboy.on('file', function (fieldname, file, filename) {

    fstream = fs.createWriteStream(__dirname + '/public/img/'+ filename);
    file.pipe(fstream);
    file.on('end', function() {
      console.log('File [' + fieldname + '] Finished sucessfully');
     });
    fstream.on('error',function(err){
      console.log('fstream error' + err);
      file.unpipe();
    });
    fstream.on('close', function () {
      res.status(200);
      res.json({ message: 'File uploaded' });

    });
  });
  req.pipe(req.busboy);

});

这是我遇到的错误

throw er; // Unhandled 'error' event
: Error: Unexpected end of multipart data
2017-05-07T20:28:27.599826+00:00 app[web.1]:     at 
/app/node_modules/busboy/node_modules/dicer/lib/Dicer.js:62:28

【问题讨论】:

  • 无关提示:永远不要按原样使用filename。它是客户端提供的,可以是任何值(包括可能是预期目标目录之外的相对路径的恶意值)。相反,使用随机文件名甚至是 filename 的哈希就可以了。
  • 感谢@mscdex 的提示
  • 使用 C# 客户端时出现完全相同的错误:“错误:c:\Code\NodeJSHW\node_modules\dicer\lib\Dicer.js:62:28 at _combinedTickCallback 的多部分数据意外结束(internal/process/next_tick.js:95:7) 在 process._tickCallback (internal/process/next_tick.js:161:9)"

标签: node.js express multer busboy


【解决方案1】:

对我来说,当我在客户端格式化我的帖子正文时使用 \n 换行符而不是 \r\n 换行符时收到此错误。

当我修复换行符时(如下面的代码所示),它起作用了。

fetch('/api/upload', 
  { method: 'POST',
    credentials: 'include',
    headers: {'Content-type': 'multipart/form-data; boundary=XXX' },
    body: '--XXX\r\nContent-Disposition: form-data; name="file"; filename="filename.csv"\r\nContent-Type: text/csv\r\n\r\nA,B,C\r\n1,1.1,name1\r\n2,2.2,name2\r\n\r\n--XXX--'
  });

【讨论】:

    【解决方案2】:

    这是一个与 firebase 工具相关的错误。我今天遇到了 busboy 包的这个问题,我花了 2 个小时来解决这个问题。我们只需要升级 firebase 工具来解决这个问题。

    案例1:如果你已经安装了firebase工具作为你的包依赖,运行下面的代码

    npm i firebase-tools
    

    案例 2: 如果您已将 firebase 工具安装为全局依赖项,请运行以下代码

    npm i -g firebase-tools
    

    firebase 工具的工作版本:

    我希望这会有所帮助,有关更多信息,请查看此问题 link

    【讨论】:

      【解决方案3】:

      如果还有其他人对此仍有疑问,那么我的问题出在前端。使用 Swift,如果您使用默认的 URLSession 配置,似乎会出现问题。我将其更改为:

      let session = URLSession(configuration: .default, delegate: self, delegateQueue: nil)
      

      到以下:

        let sessionConfig = URLSessionConfiguration.background(withIdentifier: "it.yourapp.upload")
              sessionConfig.isDiscretionary = false
              sessionConfig.networkServiceType = .default
      
              let session = URLSession(configuration: sessionConfig, delegate: self, delegateQueue: OperationQueue.main)
      

      现在效果很好!

      【讨论】:

        【解决方案4】:

        您可能会错过最终边界的结束破折号 ('--')。

        https://www.w3.org/Protocols/rfc1341/7_2_Multipart.html

        示例:

        Content-Type: multipart/alternative; boundary=boundary42 
        
        
        --boundary42 
        Content-Type: text/plain; charset=us-ascii 
        
        ...plain text version of message goes here.... 
        
        --boundary42 
        Content-Type: text/richtext 
        
        .... richtext version of same message goes here ... 
        --boundary42 
        Content-Type: text/x-whatever 
        
        .... fanciest formatted version of same  message  goes  here 
        ... 
        --boundary42-- 
        

        【讨论】:

          【解决方案5】:

          好的,我也遇到了这个问题, 我尝试在应用程序端添加此标头 'Content-Type': 'multipart/form-data;边界=XXX', 结果成功了:)

          【讨论】:

            猜你喜欢
            • 2021-10-13
            • 2019-04-04
            • 2021-05-18
            • 2012-02-10
            • 1970-01-01
            • 2021-05-28
            • 2018-06-14
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多