Thomas Depole
Thomas Depole

Reputation: 859

Can't upload two or more files using FTPClient node.js module

I'm using node, and the node-ftp module. I need to upload two files to another server, I'm able to upload one file, but when I try uploading two files it throws back and error.

according to their api, this is the code for sending a file

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

which works fine, but if i want to do two file I assume I would have to just repeat that function, but it won't work. Does anyone know how to send two or more files using FTPClient

here is the the code I'm trying to execute

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

update: I tried this as well, but it won't connect again.

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

Upvotes: 1

Views: 3847

Answers (2)

Sparkida
Sparkida

Reputation: 422

I'm the author of FTPimp, which unlike any other Node FTP module, FTPimp has a proprietary queue that runs everything asynchronously and sequentially, so you can avoid callback hell and coupling dependencies via the following:

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

and since FTPimp.put() passes the filename to the callback, your logs are a bit simpler as well :) cheers

Upvotes: 1

JohnnyHK
JohnnyHK

Reputation: 311835

Don't end the connection until both files are uploaded in series:

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

Upvotes: 2

Related Questions