Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting a progress for an FTP-upload with node

I have an Electron app which uploads a dropped file to a predefined server with node-ftp. The upload works like a charm, but despite reading a couple of suggestions I cannot figure out how to get information on the actual progress for a progress-bar. My upload-code so far:

var ftp = new Client();
let uploadfile = fs.createReadStream(f.path);
let newname = uuid(); //some function I use for renaming

ftp.on('ready', function () {
    ftp.put(uploadfile, newname, function (err) {
        if (err) throw err;
        ftp.end();
    });
});

c.connect({user: 'test', password: 'test'});

I always stumble across monitoring the 'data' event, but could not find out how or where to access it (as you can see I'm quite new to JavaScript).

like image 893
Torf Avatar asked Jan 19 '17 09:01

Torf


People also ask

How do I upload files to an FTP automatically?

Setting up automatic FTP scheduling is as easy as right-clicking on the folder or directory you want to schedule, and clicking Schedule. In the Task Scheduler section you'll be able to name the task, and set a date and time for the transfer to occur.

What is FTP in node JS?

This is an FTP client library for Node. js. It supports FTPS over TLS, Passive Mode over IPv6, has a Promise-based API, and offers methods to operate on whole directories.


1 Answers

Got it. I found the answer in streams with percentage complete

With my code changed to

var ftp = new Client();
let uploadfile = fs.createReadStream(f.path);
let newname = uuid(); //some function I use for renaming

ftp.on('ready', function() {
    uploadfile.on('data', function(buffer) {
        var segmentLength = buffer.length;
        uploadedSize += segmentLength;
        console.log("Progress:\t" + ((uploadedSize/f.size*100).toFixed(2) + "%"));
    });

    ftp.put(uploadfile, newname, function(err) {
        if (err) throw err;
            ftp.end();
    });
});
c.connect({user: 'test', password: 'test'});

I get the percentage uploaded in console. From here it's only a small step to a graphical output.

like image 197
Torf Avatar answered Oct 17 '22 00:10

Torf