我正在使用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();
});
});
答案 0 :(得分: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();
});
});
}
);
答案 1 :(得分:1)
我是FTPimp的作者,与任何其他节点FTP模块不同,FTPimp有一个专有队列,可以异步和顺序运行所有内容,因此您可以通过以下方式避免回调地狱和耦合依赖:
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()
将文件名传递给回调,您的日志也更简单了:)欢呼声