carlos E.
carlos E.

Reputation: 115

Node.js sftp download file

I am using Node.js to pull files from a remote server. I want to be able to download theses files anytime I pull put the file path. I have tried:

Connection and pull file

var Client = require('ssh2-sftp-client');
var sftp = new Client();

var remotePathToList = '/archiveFiles/file.wav';

sftp.connect({
    host: '*******',
    port: '22',
    username: '******',
    password: '******'
}).then(() => {
   res.download(remotePathToList);
}).catch((err) => {
   console.log(err, 'catch error');

But I get a "ENOENT: no such file or directory, stat "C:'/archiveFiles/file.wav'". But I am trying to download theses files from the remote server not my local desktop.

Upvotes: 3

Views: 4898

Answers (1)

dzm
dzm

Reputation: 23534

Take a look at the documentation for the library you're using ssh2-sftp-client. There's a get method for retrieving files and an example here.

Here's an example of how it may look for you.

sftp.connect(config).then(() => {  
  sftp.get('/archiveFiles/file.wav').then((data) => {
    var outFile = fs.createWriteStream('file.wav')
    data.on('data',function(response) {
      outFile.write(response);
    });
    data.on('close', function() {
      outFile.close();
    });
  });
})

Upvotes: 1

Related Questions