【问题标题】:Multiple file upload using busboy (nodejs) and issue related response object使用 busboy (nodejs) 上传多个文件并发出相关响应对象
【发布时间】:2016-01-28 15:34:17
【问题描述】:

我正在使用 busboy 模块上传文件。它适用于一个文件。但是,当我尝试上传多个文件时,它会引发以下错误。

Error: Can't set headers after they are sent

我知道它为什么会抛出错误,但我无法找出解决方案。以下是我的代码 sn-p。

  exports.uploadFile = function (req, res) {
     console.log('Calling uploadFile inside FileUploadService');
     var fstream;
     req.pipe(req.busboy);

     req.busboy.on('file', function (fieldName, file, fileName) {
         // Get folderGuid
         var folderGuid = req.params.folderGuid;
         //Get folderName
         var folderName = req.query.folderName;
         //path of file contents
         var directoryPath = fileRepositoryPath + "/" + folderGuid+"/"+folderName;
         //Get location
         var filePath = directoryPath + "/" + fileName;
         log.debug("inside FileUploadService ::: uploadFile >> folderGuid:  " + folderGuid + ", directoryPath : " + directoryPath + ", filePath : " + filePath);
         //Create directory
         nodefs.mkdir(directoryPath, 0777, true, function (err) {
             if (err) {
                 log.error({err: err}, 'Error while creating recurrisve directory');
             } else {
                 log.debug('inside FileUploadService ::: uploadFile >> Directory created');
             }
             //Write object on file system
             fstream = nodefs.createWriteStream(filePath);
             file.pipe(fstream);
             fstream.on('close', function (err) {

                 if (!err) {
                     var relativePath = "/" + folderGuid + "/" + fileName;
                     log.info('Successfully uploaded file relativePath >> '+relativePath); 
                     res.status(constants.HTTP_CODE_OK);
                     res.json({"relativePath": relativePath});
                 } else {
                     log.error({err: err}, 'Failed to upload file');
                     res.status(constants.HTTP_CODE_INTERNAL_SERVER_ERROR);
                     res.json({error: err});
                 }
             });

         });
     });

 };

我知道下面这行代码会抛出错误,这是因为在多个文件的情况下,这行代码会执行两次导致错误。

 res.status(constants.HTTP_CODE_OK);
 res.json({"relativePath": relativePath});

如何跟踪“req.busboy.on('file', function (fieldName, file, fileName)”的多个事件,以便在所有文件处理完成后,只有我应该发送响应?

请帮忙。

【问题讨论】:

  • 到目前为止,您找到解决方案了吗?我面临同样的问题..
  • 我也有同样的问题。 :(

标签: node.js busboy


【解决方案1】:

Busboy 一次处理一个文件,因此根据您的代码,响应会在一个文件流式传输完成后发送。 现在,当下一个文件流式传输完成时,响应已经发送,因此您会收到此错误。

在所有文件完成后尝试发送响应。

  req.busboy.on('finish', function() {
    res.status(constants.HTTP_CODE_OK);
    res.json({"relativePath": relativePath});
  });

或者按照这个问题的答案尝试:busboy-connect fires on finish before the end of save file (node.js , express)

【讨论】:

  • 是的,确实...它会起作用的。一旦所有操作完成,Busboy 只会调用一次完成。
【解决方案2】:

您可以更改语法,包括“req.pipe(req.busboy);”在“req.busboy.on('file', function (fieldName, file, fileName) {})”之后

【讨论】:

    猜你喜欢
    • 2019-05-27
    • 2016-05-08
    • 2020-04-05
    • 2020-02-16
    • 2023-03-05
    • 2015-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多