mscdex / node-ftp

An FTP client module for node.js
MIT License
1.13k stars 243 forks source link

get method is not downloading the whole file? #183

Open inanbayram opened 7 years ago

inanbayram commented 7 years ago

I'm downloading with the following code files from my ftp server but the code is downloading sporadically not the whole file. For example I have a file on the ftp server, which have the size of 35kb (ftp server) and after downloading on the local machine only 34kb(local) and there are some datas missing.

Someone have a idea, why this happens?

var Client = require('ftp');
  var fs = require('fs');

  var c = new Client();
  c.on('ready', function() {
    c.get('foo.txt', function(err, stream) {
      if (err) throw err;
      stream.once('close', function() { c.end(); });
      stream.pipe(fs.createWriteStream('foo.local-copy.txt'));
    });
  });
  // connect to localhost:21 as anonymous
  c.connect();
creesch commented 7 years ago

I just saw this and figured I might as well comment for people reading this. This is because the close event indicates that the last data has been received but it doesn't mean that the data has been piped properly to your local copy.

Using stream.once('finish' might help you.