1

我正在使用 node 和 node-ftp 模块。我需要将两个文件上传到另一台服务器,我可以上传一个文件,但是当我尝试上传两个文件时,它会抛出错误。

根据他们的api,这是发送文件的代码

var fs = require('fs');
conn.put(fs.createReadStream('/var/www/videoComplete/'+ videoID +'.flv'), '/home/wowza/content/'+ videoID +'.flv', function(e) {
  console.log(fileName + '.flv uploaded to Streaming Server :)');
  conn.end();
});

效果很好,但是如果我想做两个文件,我假设我必须重复该功能,但它不起作用。有谁知道如何使用 FTPClient 发送两个或多个文件

这是我要执行的代码

conn = new FTPClient({ host: 'serverIP' });
conn.on('connect', function() {
  conn.auth('user', 'pass', function(e) {
    if (e) throw e;
    var fs = require('fs');

    conn.put(fs.createReadStream('/var/www/ce-videoComplete/'+ videoID +'.flv'), '/home/wowza/content/'+ videoID +'.flv', function(e) {
      console.log(fileName + '.flv uploaded to Streaming Server :)');
      conn.end();
    });

    conn.put(fs.createReadStream('/var/www/ce-thumbnails/'+ videoID +'.jpg'), '/var/www/html/thumbnails/'+ videoID +'.jpg', function(e) {
      console.log(fileName + '.jpg uploaded to Streaming Server :)');
      conn.end();
    });

  });
});
conn.connect();

更新:我也试过了,但它不会再次连接。

conn.put(fs.createReadStream('/var/www/ce-videoComplete/'+ videoID +'.flv'), '/home/wowza/content/'+ videoID +'.flv', function(e) {
  console.log(fileName + '.flv uploaded to Streaming Server :)');
  conn.end();

  conn.put(fs.createReadStream('/var/www/ce-thumbnails/'+ videoID +'.jpg'), '/var/www/html/thumbnails/'+ videoID +'.jpg', function(e) {
    console.log(fileName + '.jpg uploaded to Streaming Server :)');
    conn.end();
  });
});
4

2 回答 2

2

在两个文件都连续上传之前不要结束连接:

conn.put(fs.createReadStream('/var/www/ce-videoComplete/'+ videoID +'.flv'), 
    '/home/wowza/content/'+ videoID +'.flv',
    function(e) {
        console.log(fileName + '.flv uploaded to Streaming Server :)');

        conn.put(fs.createReadStream('/var/www/ce-thumbnails/'+ videoID +'.jpg'),
            '/var/www/html/thumbnails/'+ videoID +'.jpg', 
            function(e) {
                console.log(fileName + '.jpg uploaded to Streaming Server :)');
                // Now that both files are uploaded, end the connection.
                conn.end();
            });  
        });
    }
);
于 2012-08-28T18:03:47.853 回答
1

我是FTPimp的作者,与任何其他 Node FTP 模块不同,FTPimp 有一个专有队列,可以异步和顺序地运行所有内容,因此您可以通过以下方式避免回调地狱和耦合依赖:

FTPimp.put

ftp.put(['/var/www/ce-videoComplete/'+ videoID +'.flv', '/home/wowza/content/'+ videoID +'.flv'], function (err, filename) {
    console.log(fileName + 'uploaded to Streaming Server :)');
});
ftp.put(['var/www/ce-thumbnails/'+ videoID +'.jpg', '/var/www/html/thumbnails/'+ videoID +'.jpg'], function(err, filename) {
    console.log(fileName + ' uploaded to Streaming Server :)');
    ftp.quit();
});

并且由于FTPimp.put()将文件名传递给回调,因此您的日志也更简单了:) 干杯

于 2015-10-15T14:50:11.000 回答