Download a file from NodeJS Server using ExpressJS

I this tutorial you will learn how to download files from your NodeJS server from your express js application. As your application is built using the ExpressJS framework you don’t have to play around with HTTP headers to allow downloads form your Node server. Express providing Helper function called res.download(path [, filename] [, fn]); It transfers the file at path as an “attachment”. Using this function you can easily allow file downloads from your application.

Download a file from NodeJS Server using ExpressJS

This function, transfers the file at path as an "attachment". Typically, browsers will prompt the user for download. By default, the Content-Disposition header "filename=" parameter is path (this typically appears in the browser dialog). Override this default with the filename parameter.

When an error occurs or transfer is complete, the method calls the optional callback function fn. This method uses res.sendFile() to transfer the file.

Script to Download a file

Step 1: Create a package.json file and install dependencies.

Step 2: create a file called index.js in root expressjs-download/index.js of project. And create a express server with route, it should accept file name in the URL.

Step 3: Run the application by issuing node index.js command from your terminal, you should be able to see the output "application is running at: http://localhost:8000" in your console and if you head over to the browser with http://localhost:8000/download/{filename}, you will get download prompt.

Here {filename} = name of the file including extension ( it will look for the file in expressjs-download/uploads directory)

Other resources

Getting Started with Express.js
Content-Disposition
How to serve static files with Express
File uploads in express js 4

I hope you like this Post, Please feel free to comment below, your suggestion and problems if you face - we are here to solve your problems.

0 0 vote
Article Rating
Subscribe
Notify of
guest
5 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Arnab Chakraborty
Arnab Chakraborty
2 years ago

Hi Arjun Res.download for me not openning save prompt. And Cant download file. The file is not in Local node project directory, It is in another outside directory.

Arnab Chakraborty
Arnab Chakraborty
2 years ago

here is my code :-
App.js
app.get(‘/download’,fileHandlerClassClass.download);

In Controller.js
function downloadDesiredFile(fileName){
pathfile = ‘/Users/arnab/Desktop/uploads’;//Path for destination directory
FileNameToDownload = fileName;
var params = FileNameToDownload;
var xhttp = new XMLHttpRequest();

};
xhttp.open(“GET”, “/download/”+params, true);
xhttp.setRequestHeader(“Content-type”, “application/octet-stream”);
xhttp.send();
}

And in Handler.js

exports.download = function (req, res) {
var fs = require(‘fs’);
var file = req.params.file;
var path = require(‘path’);
path = ‘/Users/arnab/Desktop/uploads/’;
console.log(path, file);
res.download(path, file);
};

Arnab Chakraborty
Arnab Chakraborty
2 years ago

What I am trying to achieve it I have a list of file in directory. Each has its own path. And download button beside it. I need to download it by button click

Swapnil Sawant
Swapnil Sawant
2 years ago

just use the anchor tag instead of button and see where the url points to just paste the intial path instead of the path mentioned in above code it works

Alex
Alex
1 month ago

Hi Arjun, Thank you for your post, downloading works perfectly. What is the advantage to have asterisk in brackets in ‘/download/:file(*)’ ? It also works without it as ‘/download/:file’

DMCA.com Protection Status
5
0
Would love your thoughts, please comment.x
()
x