【问题标题】:Upload file in node.js and POST multipart in spring boot在 node.js 中上传文件并在 Spring Boot 中发布多部分
【发布时间】:2016-09-20 18:42:47
【问题描述】:

如何通过http发送在node.js(多部分)中上传的文件,如下所示:

var options = {
  host: url,
  port: 8080,
  path: '/sendFile',
  method: 'POST'
};

http.request(options, function(res) {
  console.log('STATUS: ' + res.statusCode);
  console.log('HEADERS: ' + JSON.stringify(res.headers));
  res.setEncoding('utf8');
  res.on('data', function (chunk) {
    console.log('BODY: ' + chunk);
  });
}).end();

在http请求中,如何发送分段上传的文件?

【问题讨论】:

    标签: javascript node.js express


    【解决方案1】:

    这取决于。有多种策略。

    最简单的方法是使用请求中的 .form() 功能来使用正确的,例如:

    var form = req.form();
    form.append('file', file_content, {
    filename: 'myfile.ext',
    contentType: 'corresponding content/type'
    });
    

    然后做post请求

    ...或者您可以将其流式传输并让请求提取所需的数据

    form.append('file', fs.createReadStream('path/to/file'));
    

    【讨论】:

      猜你喜欢
      • 2014-10-31
      • 2014-05-01
      • 2022-10-22
      • 1970-01-01
      • 2019-11-10
      • 2017-11-04
      • 2021-05-29
      • 2021-08-05
      • 2019-07-24
      相关资源
      最近更新 更多