我正在尝试将 SFTP 服务器位置中的 xml 文件下载到我的本地计算机位置。我使用了 npm 包 ssh2-sftp-client & 使用 sftp.get 函数。服务器文件位置保存在文本文件中,例如:
/opt/path/xml1,
/opt/path/xml2
这些路径使用fs.readfile
函数读取并存储在数组中,这意味着
arr[1] = /opt/path/xml1
arr[2] = /opt/path/xml2
然后使用sftp.get
&stream.pipe
在本地路径中下载文件。问题是 xml1 只是被下载&之后代码继续运行&超时。知道为什么它没有运行多次吗?
用 Javascript 编码。
// Reading xml file location from text file
// array has two remotefile location
for(var i = 0; len = arr.length; i < len; i++)
{
(function (i)
{
var remotefilename = arr[i];
var localfilename = '/path';
sftp.get(remotefilename).then(stream) =>
{
stream.pipe(fs.createWriteStream(localfilename));
// it download xml1 only after that keeps running & timing out
// I suspect not coming out of this loop. Have tried stream.end/close, fs.close but didn't working
})
})(i);
}