Downloading multiple file from ftp site using node

2019-06-25 13:41发布

i am trying to download each file on ftp server from root folder. what i did is this-

ftpClient.ls(".", function(err, res) {
      res.forEach(function(file) {
          console.log(file.name);

            ftpClient.get("./"+file.name, 'D:/styleinc/ftp/'+file.name, function(hadErr) {
                if (hadErr)
                  console.log(hadErr);
                else
                  console.log('File copied successfully!');

      });
    });

but on running it gives me error-

{ [Error: connect ECONNREFUSED]
   code: 'ECONNREFUSED',
   errno: 'ECONNREFUSED',
   syscall: 'connect',
   msg: 'Probably trying a PASV operation while one is in progress' 
  }

i have already successfully logged in and authenticated my self on ftp site..... i don't know what to do please guide me.

标签: node.js ftp
1条回答
Ridiculous、
2楼-- · 2019-06-25 14:09

This is the chunk of code I used with async.mapLimit to make it work with only one connection concurrently.

'use strict'

var JSFtp = require('jsftp');
var inspect = require('util').inspect;
var fs = require('fs');
var async = require('async');

var ftp = new JSFtp(require('./util/ftp'))
var local = 'EDI/mohawk/OUTBOX/'
var remote = 'OUTBOX'

var gatherFiles = function(dir){
  return new Promise(function(resolve, reject){
    ftp.ls(dir + '/*', function(err, res) {
      if (err) reject(err)
      console.log(res)
      var files = [];
      res.forEach(function(file){
        files.push(file.name)
      });
      resolve(files)
    })
  })
}

gatherFiles(remote).then(function(files){
  console.log(files)
  async.mapLimit(files, 1, function(file, callback){
    console.log('attempting: ' +remote + file + '->' + local + file)
    ftp.get(remote +'/'+ file, local +'/'+ file, function(err){
      if(err){
        console.log('Error getting ' + file)
        callback(err)
      }else{
        console.log('Got ' + file)
        callback()
      }

    })
  }, function(err, res){
    if(err){
      console.log(err)
    }
    console.log('updates complete' + res)   
  })
})
查看更多
登录 后发表回答