0

I have an endpoint to which I post a .zip file and a .xlsx file at the same time.

Client (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;
  }
);

Server (Node):

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);

Now here comes the curious part: it works 100% when running locally, but when it's running on a remote server, it responds with net::ERR_CONNECTION_RESET. The error object does print out, but it prints out what looks to be the busboy object, with no useful information that I've found as to why the connection was closed.

This is a partial bit of the object: {upload: f, progress: 81, name: "test-xlsx.xlsx", lastModified: 1507565920197, lastModifiedDate: Mon Oct 09 2017 09:18:40 GMT-0700 (PDT)}

So as we can see it made some progress. And on the server there exists the .zip file, but it isn't complete, as an unzip operation on it fails.

Let me know if any more information would help! Thanks!

Edit: The .zip file is 197KB and the .xlsx file is 7KB, when testing locally, it worked with zip files at least as large as 120MB.

Big Money
  • 9,139
  • 6
  • 26
  • 37

1 Answers1

0

I figured it out. It turned out to be an issue with pm2. We had pm2 running on the server with the watch command. So, every time something in the server files changed, it restarted. It was writing the files to disk, so once pm2 saw the .zip archive, it restarted and halted the connection.

Big Money
  • 9,139
  • 6
  • 26
  • 37