为什么这与流式阅读器形成附件;我真的不管用

Why this forms attachment with stream reader doesn't work truly?

本文关键字:真的 不管 为什么      更新时间:2023-09-26

我想读取一个图像并将其附加到请求主体,如下所示:

// Sending files to server by adding and posting theme
// 1. loop through an array of file path pair ['file', 'path' ]
// 2. pass result to request function an post it

一些代码如下:

forms = [];
len = 3;
while (len > 0){
  forms.push({ 'fileName': fs.createReadStream (path) })
  len--
  }
async.each(forms, function(form) {
  request.post( { url : url, formData: form }, function(err, response) {
               console.log(response.body)
  });
});

只有一个请求和一个表单元素。

您应该能够使用Array.prototype.forEach()

forms.forEach(form => 
  request.post( { url : url, formData: form }
  , (err, response) => console.log(response.body))
);