我正在尝试从服务器下载文件。我可以连接到sftp,列出给定目录中的所有文件。但是,下载后的文件似乎已损坏。我打不开它。另外,我在下载时遇到错误,如下所示
UnhandledPromiseRejectionWarning: TypeError: data.on is not a function 以下是代码
const Client = require('ssh2-sftp-client');
let sftp = new Client;
sftp.connect(config).then(() => {
sftp.get(remotePath).then((data) => {
var outFile = fs.createWriteStream('fileee.zip')
outFile.on('data',function(response) {
outFile.write(response);
});
outFile.on('close', function() {
outFile.close();
});
});
})发布于 2021-06-23 15:13:43
引用自docs
let client = new Client();
let remotePath = '/remote/server/path/file.txt';
let dst = fs.createWriteStream('/local/file/path/copy.txt');
client.connect(config)
.then(() => {
return client.get(remotePath, dst);
})
.then(() => {
client.end();
})
.catch(err => {
console.error(err.message);
});不知道你从哪里得到关于data.on()的想法..。
https://stackoverflow.com/questions/68094850
复制相似问题