无法使用FTPClient node.js模块上传两个或更多文件

我正在使用节点和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(); }); 

这工作正常,但如果我想要做两个文件,我假设我将不得不重复该function,但它不会工作。 有谁知道如何使用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(); }); }); 

Solutions Collecting From Web of "无法使用FTPClient node.js模块上传两个或更多文件"

直到两个文件都以一系列的方式上传后才能结束连接:

 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(); }); }); } ); 

我是FTPimp的作者, FTPimp不同于任何其他的FTP FTP模块,FTPimp拥有一个专有的队列,可以asynchronous和顺序地运行所有的东西,所以你可以通过下面的方式来避免callback地狱和耦合依赖:

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()将文件名传递给callbackFTPimp.put() ,所以你的日志也会更简单一点:)干杯