【问题标题】:ftp client with async javascript带有异步 javascript 的 ftp 客户端
【发布时间】:2015-08-22 23:31:56
【问题描述】:

我正在使用 node.js 并使用 async.js 和 ftp 客户端模块。
我正在使用 async.series,这是 async.series 中的一个函数:

    var client = new ftp();
client.on('ready', function () {
    for ( var i = 0  ; i < stage._sub_directories.length ; i++ ) {
        var path = get_absolute_path() + DIRECTORY_NAME_LESSONS + stage._name + "/" + DIRECTORY_NAME_STAGE + stage._sub_directories[i];
        var file = 'copy.' + stage._sub_directories[i] + get_xml_file_name();
        client.get(path + "/" + get_xml_file_name(), function (err, stream) {
            console.log('done');
            if (err) throw err;
            stream.once('close', function () {client.end();});
            stream.pipe(fs.createWriteStream(file));
            callback(null, true);
        });
    }
});
client.connect(config);

callback(null, true); 用于异步模块。 当它运行时,它插入for 循环stage._sub_directories.length 次,然后然后 插入client.get() 一次(最后一次)。
它创建了一个文件,而不是更多。
我的问题是什么?我应该使用另一个异步控制流吗?或者是其他东西?

【问题讨论】:

    标签: javascript node.js asynchronous ftp ftp-client


    【解决方案1】:

    按照您的代码编写方式,它不起作用,因为您在常规循环中调用异步函数client.get

    您应该将常规的for 循环替换为async.eachSeries 方法,因为您已经在使用async.js。试试这个:

    client.on('ready', function () {
        async.eachSeries(stage._sub_directories, function (subDir, done) {
            var path = get_absolute_path() + DIRECTORY_NAME_LESSONS + stage._name + "/" + DIRECTORY_NAME_STAGE + subDir;
            var file = 'copy.' + subDir + get_xml_file_name();
            client.get(path + "/" + get_xml_file_name(), function (err, stream) {
                if (err) return done(err);
                console.log('done');
                stream.once('close', function () {client.end();});
                stream.pipe(fs.createWriteStream(file));
                done();
            });
        }, function (err) {
            if (err) return callback(err);
            callback(null, true);
        });
    });
    

    【讨论】:

      【解决方案2】:

      老实说,要“解决”这些当前流行的 FTP 模块需要多少标记和发现,真是令人沮丧……我是 FTPimp 的作者,我专门为解决这种沮丧而写的。 例如,您可以只使用 ftpimp 模块来做同样的事情:

      ftp.connect(function () {
          stage._sub_directories.forEach(function (subDir) {
              var path = get_absolute_path() + DIRECTORY_NAME_LESSONS + stage._name + "/" + DIRECTORY_NAME_STAGE + subDir;
              var file = 'copy.' + subDir + get_xml_file_name();
              //save from -> to
              ftp.save([path + "/" + get_xml_file_name(), file], function (err, name) {
                  console.log(err, name);
              });
          });
          //@see http://ftpimp.net/FTP.html#event:queueEmpty
          ftp.once('queueEmpty', function () {
              console.log('files saved');
              //do more stuff ...
              ftp.quit();
          });
      });
      

      【讨论】:

      • ftp.quit() 导致错误 => throw new Error('ftp.run > parameter 2 expected a callback function'); (index.js:501)。它似乎需要传递一个回调函数。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-04-24
      • 1970-01-01
      • 1970-01-01
      • 2012-09-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多