响应流写入数组[nodejs]

时间:2018-10-16 22:02:59

标签: node.js stream

我正在从响应中读取几个字节,我想将其放入文件中,我应该在哪里添加将字节写入文件的逻辑:

代码如下:

    function Download(){
          for (a=0; a< array.length; a++){   
var uri = url[a]+'/v1/file/download/h
                    console.log(uri)
                     new Promise(function (resolve, reject) {
                      request.get(uri
                      , function (error, response, body) {
                        }
                      ).on('response', function(response) {
                        response.pause();
                        response.on('readable', function() {
                          console.log("now readable");
                          var chunk;
                          while (null !== (chunk = response.read(64*1024))) {
                            response.write(chunk);
                            console.log(`Received ${chunk} bytes of data.`);
                          }

                          sampleArray.push(chunk)

                        });
                      });
                    });
                    } else {
                      console.log("inElse")
                    }
                  }
    }

0 个答案:

没有答案