我尝试将SFTP服务器位置中的xml文件下载到我的本地计算机位置。我使用过npm package ssh2-sftp-client&使用sftp.get函数。服务器文件位置保存在文本文件中,例如:
/opt/path/xml1,
/opt/path/xml2
使用fs.readfile
功能& amp;读取这些路径。存储在数组中意味着
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);
}