使用Busboy和Node的ng-file-upload在本地运行,但不在服务器

时间:2017-10-10 17:27:40

标签: angularjs node.js express ng-file-upload busboy

我有一个端点,我同时发布.zip文件和.xlsx文件。

客户(Angular 1):

files.upload = Upload.upload({
  url: '/api/files',
  data: { xlsxFile: xlsxFile, zipFile: zipFile }
});

files.upload.then(
  function (response) {
    $scope.state = 'completed';
    $scope.message = response.data.data;
  },
  function (error) {
    $scope.state = 'error';
    $scope.message = _.get(error, 'data.errors[0].meta.errorObj') || 'An unspecified error occurred, please check your files and try again.';
  },
  function (evt) {
    progress = evt.loaded / evt.total;
  }
);

服务器(节点):

const busboy = new Busboy({headers: req.headers});
var xlsxFileBuffers = [];
var zipFilePath = `${Math.random().toString(36).substring(5)}zipFile.zip`;
busboy.on('file', (fieldName, file, filename, encoding, mimeType) => {
  if (!mimeType.includes('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet') &&
    !mimeType.includes('application/vnd.ms-excel') &&
    !mimeType.includes('application/zip')
  ) return next('Invalid file type.');

  if (fieldName === 'zipFile') {
    file.pipe(fs.createWriteStream(zipFilePath));
  }
  else {
    file.on('data', function(data) {
      xlsxFileBuffers.push(data);
    });
    file.on('end', function() {
      if (!xlsxFileBuffers.length) {
        return next('Cannot read xlsx file.');
      }
    });
  }
});
busboy.on('finish', () => {
  console.log('busboy finish called');
  if (xlsxFileBuffers.length > 0) {
    console.log('we made it!');
  }
});
req.pipe(busboy);

现在出现了一个奇怪的部分:它在本地运行时100%工作,但当它在远程服务器上运行时,它会以net::ERR_CONNECTION_RESET响应。错误对象会打印出来,但它打印出看起来像是busboy对象的内容,没有找到有关连接关闭原因的有用信息。

这是对象的一部分: {upload: f, progress: 81, name: "test-xlsx.xlsx", lastModified: 1507565920197, lastModifiedDate: Mon Oct 09 2017 09:18:40 GMT-0700 (PDT)}

因此我们可以看到它取得了一些进展。并且在服务器上存在.zip文件,但它没有完成,因为对它的解压缩操作失败。

如果有更多信息有帮助,请告诉我们!谢谢!

编辑:.zip文件是197KB,.xlsx文件是7KB,在本地测试时,它使用至少120MB的zip文件。

1 个答案:

答案 0 :(得分:0)

我明白了。事实证明这是pm2的一个问题。我们使用<article> <h1 class="headings">New Mask</h1> <fieldset> <!-- Fieldsets get <legend>, not <label> --> <legend>Type of Base Plate</legend> <br> <input type="radio" name="type" value="10000" checked>High Precision<br> <input type="radio" name="type" value="20000"> Push-Pin<br> </fieldset> <fieldset> <legend>Age</legend> <input type="radio" name="age" value="1000" checked> Adult <br> <input type="radio" name="age" value="2000"> Paediatric <br> </fieldset> <fieldset> <legend>Anatomical Region</legend> <input type="radio" name="anatomical" value="100" checked> Brain<br> <input type="radio" name="anatomical" value="200"> Head, Neck and Shoulders<br> </fieldset> <fieldset> <legend>Type of Patient</legend> <input type="radio" name="patient" value="10" checked> Curative<br> <input type="radio" name="patient" value="20"> Palliative<br> <input type="radio" name="patient" value="30"> Claustrophobic<br> </fieldset> <fieldset> <legend>Accuracy</legend> <input type="radio" name="accuracy" value="1" checked> &lt; 1mm<br> <input type="radio" name="accuracy" value="2"> &lt; 2mm<br> </fieldset> <!-- Don't use a "submit" button when you aren't submitting data anywhere. And, don't use inline HTML event attribtues. Do event binding in JavaScript. --> <button type="button" value="Submit">Submit</button> <p id="answer"></p> </article>命令在服务器上运行了pm2。因此,每次服务器文件中的某些内容发生更改时,它都会重新启动。它正在将文件写入磁盘,因此一旦pm2看到.zip存档,它就会重新启动并暂停连接。