2017-10-10 32 views
0

我有一个端点,我可以同时发布一个.zip文件和一个.xlsx文件。带Busboy和Node的ng-file-upload在本地工作,但不在服务器上工作

客户端(角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,在本地进行测试时,它与zip文件一起工作至少达到120MB。

回答

0

我想通了。事实证明,这是pm2的一个问题。我们通过watch命令在服务器上运行了pm2。因此,每次服务器文件中的某些内容发生更改时,都会重新启动。它正在将文件写入磁盘,因此一旦pm2看到.zip存档,它就会重新启动并暂停连接。

相关问题