从nodejs上传大文件到另一台服务器

从nodejs上传大文件到另一台服务器

问题描述:

我有一个使用节点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

退房它展示了如何提供分块选项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); 
    })