为什么下载文件时出现错误“错误[ERR_STREAM_WRITE_AFTER_END]:写完”?

时间:2019-10-30 06:45:11

标签: javascript node.js http download

我正在尝试使用URL将存储在天蓝色blob存储中的文件下载到各个blob。

我正在下载五个blob文件,并且确实具有指向所有这些blob文件的单独网址。

下面是我的代码:

function Download()
{
  const fs = require('fs');
  const path = require('path');
  const https = require('https');
  const url = require('url');

  var sasToken = '?sv=2019-02-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx2019-10-3xxxxxxxZ&sp=rl'
  var blobUris = ['https:xxx', 'xxx', 'xxx', 'xxx', 'xxx'];

  var i;
  for(i=0; i<blobUris.length; i++)
  {
    var sasUrl = blobUris[i] + sasToken;
    var filename=url.parse(blobUris[i]).pathname.split('/').pop();
    var filePath = "D:/BLESensor"
    var file = fs.createWriteStream(filePath+"/"+filename)
    var request = https.get(sasUrl, function(response) {
      response.pipe(file);
        //  Promise.resolve()
        //  .then(() => file.destroy())
        //  .catch(console.log)
  });
}

  }
return Download();

运行函数时出现以下错误:

Error [ERR_STREAM_WRITE_AFTER_END]: write after end
    at writeAfterEnd (_stream_writable.js:248:12)
    at WriteStream.Writable.write (_stream_writable.js:296:5)
    at IncomingMessage.ondata (_stream_readable.js:709:20)
    at IncomingMessage.emit (events.js:198:13)
    at IncomingMessage.Readable.read (_stream_readable.js:504:10)
    at flow (_stream_readable.js:973:34)
    at resume_ (_stream_readable.js:954:3)
    at process._tickCallback (internal/process/next_tick.js:63:19)
Emitted 'error' event at:
    at errorOrDestroy (internal/streams/destroy.js:107:12)
    at WriteStream.onerror (_stream_readable.js:733:7)
    at WriteStream.emit (events.js:203:15)
    at errorOrDestroy (internal/streams/destroy.js:107:12)
    at writeAfterEnd (_stream_writable.js:250:3)
    at WriteStream.Writable.write (_stream_writable.js:296:5)
    [... lines matching original stack trace ...]
    at flow (_stream_readable.js:973:34)

任何建议将不胜感激。谢谢!

1 个答案:

答案 0 :(得分:2)

这是由于以下事实:我们在https.get调用(如所有Node.js I / O调用)是异步的同时在for循环中下载。因此,在这种情况下,使用功能范围内的文件变量(例如var file ..),response.pipe无法正常工作(因为文件流将在response.pipe被调用时关闭)。

此问题有一个简单的解决方法,我们只需要确保文件变量为block scoped,例如为此使用letconst

function Download()
{
  const fs = require('fs');
  const path = require('path');
  const https = require('https');
  const url = require('url');

  var sasToken = '?sv=2019-02-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx2019-10-3xxxxxxxZ&sp=rl'
  var blobUris = ['https:xxx', 'xxx', 'xxx', 'xxx', 'xxx'];

  var i;
  for(i=0; i<blobUris.length; i++)
  {
    var sasUrl = blobUris[i] + sasToken;
    var filename=url.parse(blobUris[i]).pathname.split('/').pop();
    var filePath = "D:/BLESensor"
    // Make sure file variable is block scoped. We could use const too.. but not var!
    let file = fs.createWriteStream(filePath+"/"+filename)
    console.log("Downloading file: " + (i+1));
    var request = https.get(sasUrl, function(response) {
        response.pipe(file);
    });
  }

}

return Download();