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

时间:2014-10-16 12:53:34

标签: javascript node.js file-upload node-webkit

我有一个使用node webkit的桌面应用程序,我需要能够将大文件从节点服务器上传到另一台服务器。它需要能够将文件块化到服务器,因为有一个请求大小限制阻止流式传输整个文件。我目前正在使用请求模块发布上传而不分块,这适用于小文件,但我似乎无法找到任何关于如何从节点进行分块上传的示例。以下是我现在所拥有的:

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

知道如何在节点中完成此操作吗?我已经看到很多关于在节点服务器上接收分块上传的例子,但似乎无法找到关于如何从节点发送分块的任何内容。

1 个答案:

答案 0 :(得分: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);
  })
相关问题