Create write stream on ftp download and check if the file exists


#1

Hi!
i’m having an issue with ftp downloading.
i’m using node-ftp as ftp client, which connects (with no issue) to my client for downloading the file.

// assume that clientFTP is connected

clientFTP.on('ready', function() {

  clientFTP.size(path.join(ftpfolder, filename), function(err, bytes) {
    //console.log("TOTAL BYTES", (bytes / 1024) / 1024);
  })
  clientFTP.get(path.join(ftpfolder, filename), function(err, stream) {
    if (err) {
      throw err;
    }
    //console.log("STREAM ",stream);
    stream.once('close', function() {
      clientFTP.end();
    })
    w = fs.createWriteStream(path.join(dataPath, "data", filename));
    stream.pipe(w);
    w.on('finish',function(){
       //open the file
       //HERE IS THE PROBLEM
       file = openFile(path.join(dataPath, "data", filename))
       //LOG file returns UNDEFINED
       console.log(file)
       })
   })
})