I am using node-ftp package for FTP connection. Below code example
const FTPClient = require('ftp');
const fs = require("fs");
let ftp_client = new FTPClient();
let ftpConfig = {
host: '94.208.170.184',
port: 21,
user: '99*******',
password: '******'
}
var downloadList = [];
//create a connection to ftp server
ftp_client.connect(ftpConfig);
//list directory and files from server.
ftp_client.on('ready', function() {
ftp_client.list('xmlfiles',function(err, list) {
if (err) throw err;
list.map(function(entry){
console.log(entry.name);
if (entry.name !== '.' || entry.name !== '..') {
downloadList.push(entry.name);
}
});
downloadList.map(function(file, index){
console.log(file);
// Download remote files and save it to the local file system:
ftp_client.get('xmlfiles/' + file, function(err, stream) {
if (err) throw err;
stream.once('close', function() { ftp_client.end(); });
stream.pipe(fs.createWriteStream(file));
});
});
ftp_client.end();
});
});
Is their is any way we can download files using batch process
I have modified the code and I am able to achieve download functionality.
Below are code sample.
const FTPClient = require('ftp');
const fs = require("fs");
let ftpConfig = {
host: '94.208.170.184',
port: 21,
user: '99*******',
password: '******'
}
let downloadPathDir = path.join(__dirname, '../assets/download-xml/');
var downloadList = [];
const ftpConnection = async (callback) => {
try{
let ftp_client = new FTPClient();
ftp_client.on('error', function(e) {
console.log(`Error in ftp connection: ${e}`);
});
ftp_client.on('ready', function() {
ftp_client.list('/xmlfiles',function(err, list) {
if (err){
console.log(`Error in gracenote file listing: ${err}`);
}
if (!err){
list.map(function(entry){
if (entry.name !== '.' && entry.name !== '..' && typeof entry.name !== 'undefined') {
downloadList.push(entry.name);
}
});
callback(downloadList);
}
});
});
ftp_client.end();
ftp_client.connect(ftpConfig);
} catch (error) {
console.log(error);
}
}
const fileDwonload = (allFileNames,fileCount) => {
let currentXmlFile = allFileNames[fileCount];
let fileDtl = {"orgFile":currentXmlFile,"upFile":removeWhiteSpace(currentXmlFile)};
let xmlfile = downloadXMLFile(fileDtl);
xmlfile.then(function (data) {
let tempFileCount = allFileNames.length;
let newFileCount = fileCount + 1;
if(newFileCount < tempFileCount){
fileDwonload(allFileNames,newFileCount);
}
});
}
let downloadFile = {
callDownload : async function() {
ftpConnection(function(allFileNames) {
fileDwonload(allFileNames,0);
});
}
}
const downloadXMLFile = async function (fileDtl) {
try{
let fileName = fileDtl['orgFile'];
return new Promise((resolve, reject) => {
if (!fs.existsSync(downloadPathDir)) {
fs.mkdirSync(downloadPathDir);
}
let xmlfilepath = downloadPathDir + fileName;
let upfilepath = fileDtl['upFile'];
if (!fs.existsSync(xmlfilepath)) {
let d = new FTPClient();
d.on('error', function(e) {
console.log(`Connection Error While Downloading: ${e}`);
});
d.on('ready', function() {
if (fileName !== '.' && fileName !== '..' && typeof fileName !== 'undefined') {
d.get(`xmlfiles/${fileName}`, function(err, stream) {
if (err){
console.log(`XML download Failed: ${err}`);
}
if(!err){
stream.once('close', function() {
d.end();
});
stream.pipe(fs.createWriteStream(downloadPathDir+ upfilepath ,{flags: 'w'}));
}
});
}
});
d.end();
d.connect(ftpConfig);
}
});
}catch (err) {
console.log(`XML download Failed Connection: ${err}`);
}
};
If anyone has better solution. Feel free to update here.