gpt4 book ai didi

javascript - 为什么我在下载文件时收到错误 "Error [ERR_STREAM_WRITE_AFTER_END]: write after end"?

转载 作者:行者123 更新时间:2023-11-30 13:49:12 27 4
gpt4 key购买 nike

我正在尝试使用各个 blob 的 url 下载存储在 azure blob 存储中的文件。

我正在下载五个 blob 文件,并且我确实有所有这些 blob 文件的单独 URL。

下面是我的代码:

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)

如有任何建议,我们将不胜感激。谢谢!

最佳答案

这是因为我们在 for 循环中下载,而 https.get 调用(像所有 Node.js I/O 调用一样)是异步的。因此,在这种情况下,response.pipe 将无法使用函数范围的文件变量(例如 var file..)正常工作(因为文件流将在调用 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();

关于javascript - 为什么我在下载文件时收到错误 "Error [ERR_STREAM_WRITE_AFTER_END]: write after end"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58619903/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com