2

I am noob in Node.js. I try to download all files from directory using PromiseFtp

But I have a problem. I don't know how to begin downloading files. I make next:

var ftp = new PromiseFtp();
  ftp.connect({host: '------', user: '----------', password: '------------'})
  .then(function (serverMessage) {
    console.log('Server message: '+serverMessage);
    return ftp.list('/');
  }).then(function (list) {
    list.forEach(function (element, index, array) {
        if (element.name !== '..' && element.name !== '.'){
            console.log(element.name)
        }
    })

  }).then(function () {
    return ftp.end();
  });

Now I have list of files. But I don't know what I must do. After downloading I must delete all files from ftp.

Please. Help me

Hizqeel
  • 947
  • 3
  • 20
  • 23
user2497209
  • 45
  • 1
  • 6

1 Answers1

0

This is an example for downloading:

  var PromiseFtp = require('promise-ftp');
  var fs = require('fs');

  var ftp = new PromiseFtp();
  ftp.connect({host: host, user: user, password: password})
  .then(function (serverMessage) {
    return ftp.get('foo.txt');
  }).then(function (stream) {
    return new Promise(function (resolve, reject) {
      stream.once('close', resolve);
      stream.once('error', reject);
      stream.pipe(fs.createWriteStream('foo.local-copy.txt'));
    });
  }).then(function () {
    return ftp.end();
  });

And you can delete using the delete functionality:

delete(path ): Deletes the file at path. Returned promise resolves to undefined.

Try to write the download functionality instead of that console.log and on the callback of the download/store code call delete.

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
  • Yes, something like that. You iterate the set of files and for each element, you call ftp.get(). In the callback of each, you store it and after saving, but before ftp.end, you can remove it. – Lajos Arpad May 05 '16 at 17:34
  • I'm sorry, but I still do not get to do it. The script is running well, but files not downloading – user2497209 May 05 '16 at 18:03
  • @user2497209, are you sure you are trying to do that on the right path? Also, are you sure that you have the necessary privileges to read them? – Lajos Arpad May 05 '16 at 18:27
  • @user2497209, the best way to resolve the issue is to debug (http://stackoverflow.com/questions/1911015/how-do-i-debug-node-js-applications), since the problem may be something very simple, but difficult to see without debugging. – Lajos Arpad May 05 '16 at 18:53