正在将大型文件从nodejs上载到另一台服务器

Uploading large files from nodejs to another server

本文关键字:服务器 一台 上载 大型 文件 nodejs      更新时间:2023-09-26

我有一个使用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'});

知道我将如何在node中完成这项工作吗?我已经看到了很多在节点服务器上接收分块上传的例子,但似乎找不到任何关于如何从节点发送它们的信息。

查看请求的文档,它显示了如何提供分块选项:

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