I am currently attempting to download a file from an FTP server using ssh2-sftp-client.
I can see the list of the files as shown in the code below, my problem is when it es to downloading the files.
As you can see from my code below I am attempting to use the sftp.get to get the contents of the file and node file system to create a file.
When the file saves it doesn't save the contents of the file on the server is only saves [object Object]
var Client = require('../../node_modules/ssh2-sftp-client');
var sftp = new Client();
var root = '/files';
var fs = require('fs');
sftp.connect({
host: '192.168.0.1',
port: '22',
username: 'user',
password: 'password'
}).then(() => {
return sftp.list(root);
}).then((data) => {
for( var i = 0, len = data.length; i < len; i++) {
console.log(data[i].name);
var name = data[i].name;
sftp.get(root+'/'+name).then((file) => {
console.log(file);
fs.writeFile('downloads/'+name, file, function(err) {
if(err) {
return console.log(err);
}
console.log("The file was saved!");
});
}) }
}).catch((err) => {
console.log(err, 'catch error');
});
How can I get this to save the contents of the file?
Any help or a push in the right direction would be very appreciated.
I am currently attempting to download a file from an FTP server using ssh2-sftp-client.
I can see the list of the files as shown in the code below, my problem is when it es to downloading the files.
As you can see from my code below I am attempting to use the sftp.get to get the contents of the file and node file system to create a file.
When the file saves it doesn't save the contents of the file on the server is only saves [object Object]
var Client = require('../../node_modules/ssh2-sftp-client');
var sftp = new Client();
var root = '/files';
var fs = require('fs');
sftp.connect({
host: '192.168.0.1',
port: '22',
username: 'user',
password: 'password'
}).then(() => {
return sftp.list(root);
}).then((data) => {
for( var i = 0, len = data.length; i < len; i++) {
console.log(data[i].name);
var name = data[i].name;
sftp.get(root+'/'+name).then((file) => {
console.log(file);
fs.writeFile('downloads/'+name, file, function(err) {
if(err) {
return console.log(err);
}
console.log("The file was saved!");
});
}) }
}).catch((err) => {
console.log(err, 'catch error');
});
How can I get this to save the contents of the file?
Any help or a push in the right direction would be very appreciated.
Share Improve this question asked Aug 22, 2017 at 9:04 Tony HenslerTony Hensler 1,4926 gold badges16 silver badges30 bronze badges2 Answers
Reset to default 6I resolved my problem by changing the sftp.get to the following:-
sftp.get(root+"/"+name).then((stream) => {
stream.pipe(fs.createWriteStream(local+name));
});
I hope this helps anybody else who might have this issue.
You can pass filename with absolute path as second argument in sftp.get()
function. Here is the code snippet using async/await:
let sftp = new SFTPClient();
let YOURFILE = path.join(process.cwd(), "YOURFILENAME.EXTENSION");
try {
const connection = await sftp.connect({
host: "YOURHOST",
port: "PORT",
username: "USERNAME",
password: "PASSWORD",
});
const fileList = await sftp.list("/");
console.log("file list ====>>> ", fileList);
await sftp.get(fileList[0].name, YOURFILE);
sftp.end();
console.log("closing sftp connection");
} catch (err) {
console.log(
"Error while connecting to sftp server or fetching files from sftp server====>>>> ",err);
}