2017-10-10 31 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存檔,它就會重新啓動並暫停連接。

相關問題