2014-10-16 232 views
3

我有一個使用節點webkit的桌面應用程序,我需要能夠從節點服務器上傳大文件到另一臺服務器。它需要能夠將文件分塊到服務器,因爲請求大小限制會阻止流式傳輸整個文件。我目前使用請求模塊發佈上傳沒有分塊,這適用於小文件,但我似乎無法找到任何關於如何從節點進行分塊上傳的例子。以下是我在這一點上:從nodejs上傳大文件到另一臺服務器

var form = request.post('http://server.com/Document/Upload', 
    {contentType: 'multipart/form-data; boundary="' + boundaryKey + '"', preambleCRLF: true, postambleCRLF: true}, 
    function(err, res, body) { 
     console.log(res); 
    }).form(); 

form.append('uploadId', myUploadId); 
form.append('file', fs.createReadStream(zipFileFullPath), {filename: 'test.zip'}); 

任何想法如何在節點中完成此操作?我已經看到了很多在節點服務器上接收分塊上傳的例子,但似乎無法找到關於如何從節點發送它們的任何內容。

+0

什麼是錯的代碼,你已經證明? – mscdex 2014-10-16 13:59:01

+0

問題是它沒有塊文件,所以當我發送超過服務器上最大請求長度大小的文件時,它會失敗。 – dwilliams 2014-10-16 14:02:22

+0

如果是這樣的話,您是如何期待「分塊」文件的?多重要求? – mscdex 2014-10-16 14:10:43

回答

1

退房它展示瞭如何提供分塊選項request的文檔:

request({ 
    method: 'PUT', 
    preambleCRLF: true, 
    postambleCRLF: true, 
    uri: 'http://service.com/upload', 
    multipart: [ 
     { 
     'content-type': 'application/json' 
     body: JSON.stringify({foo: 'bar', _attachments: {'message.txt': {follows: true, length: 18, 'content_type': 'text/plain' }}}) 
     }, 
     { body: 'I am an attachment' }, 
     { body: fs.createReadStream('image.png') } 
    ], 
    // alternatively pass an object containing additional options 
    multipart: { 
     chunked: false, 
     data: [ 
     { 
      'content-type': 'application/json', 
      body: JSON.stringify({foo: 'bar', _attachments: {'message.txt': {follows: true, length: 18, 'content_type': 'text/plain' }}}) 
     }, 
     { body: 'I am an attachment' } 
     ] 
    } 
    }, 
    function (error, response, body) { 
    if (error) { 
     return console.error('upload failed:', error); 
    } 
    console.log('Upload successful! Server responded with:', body); 
    }) 
相關問題