Sonu Sen Sonu Sen - 3 months ago 54
Node.js Question

file download from ftp server in nodejs

I have web application where on button click invoking REST API which fetch list of files available in ftp server and displayed in div with hyperlink on file name as below.

for this I am using jsftp module.

ftp.ls(".", function(err, res) {
res.forEach(function(file) {
console.log(file.name);
//logic to display in div
});
});


I have another REST API for download file from FTP server which invoked on file name click present in div but it is downloaded in local directory where web application deploy and not in users system with below code, but I want when user click on file name that file should download in users system or computer. How I can achieve this.

ftp.get('remote/file.txt', 'local/file.txt', function(hadErr) {
if (hadErr)
console.error('There was an error retrieving the file.');
else
console.log('File copied successfully!');
});

Answer

Now that the file is on your machine you will want to actually server the file to the client when they click on the link. Using res.write something like: res.write(file, 'binary');

Here are some concepts that you'll find useful: Node.js send file to client