Download a file from NodeJS Server using Express

0 votes

How can I download a file from my server to a computer that is browsing a nodeJS server page?

I'm using ExpressJS and have been experimenting with the following:

app.get('/download', function(req, res){

  var file = fs.readFileSync(__dirname + '/upload-folder/dramaticpenguin.MOV', 'binary');

  res.setHeader('Content-Length', file.length);
  res.write(file, 'binary');
  res.end();
});

But I can't get the file name and the file type ( or extension ). Can anyone help me with that?

Jun 13, 2022 in Node-js by Vaani
• 7,070 points
7,593 views

1 answer to this question.

0 votes

Because the file's name is just 'download' in your browser's eyes, you'll need to offer it extra information by using another HTTP header.

res.setHeader('Content-disposition', 'attachment; filename=dramaticpenguin.MOV');

You may also want to send a mime-type such as this:

res.setHeader('Content-type', 'video/quicktime');

If you want something more in-depth, here ya go.

var path = require('path');
var mime = require('mime');
var fs = require('fs');

app.get('/download', function(req, res){

  var file = __dirname + '/upload-folder/dramaticpenguin.MOV';

  var filename = path.basename(file);
  var mimetype = mime.lookup(file);

  res.setHeader('Content-disposition', 'attachment; filename=' + filename);
  res.setHeader('Content-type', mimetype);

  var filestream = fs.createReadStream(file);
  filestream.pipe(res);
});

You may change the header value to anything you like. In this scenario, I'm checking the file's mime-type with a mime-type library called node-mime.

Another thing to keep in mind is that I've switched your code to utilise a readStream. Because node is designed to be asynchronous, employing any method with the word 'Sync' in the name is frowned upon.

To make things easier, Express includes a tool for this.

http://expressjs.com/en/api.html#res.download

app.get('/download', function(req, res){
  const file = `${__dirname}/upload-folder/dramaticpenguin.MOV`;
  res.download(file); // Set disposition and send it.
});
answered Jun 13, 2022 by Neha
• 9,020 points

Related Questions In Node-js

0 votes
1 answer

How to extract request http headers from a request using NodeJS connect?

Hello @kartik, To see a list of HTTP ...READ MORE

answered Jul 15, 2020 in Node-js by Niroj
• 82,840 points
23,222 views
0 votes
1 answer

How to download a file with Node.js without using third-party libraries?

Hii, You can create an HTTP GET request and pipe ...READ MORE

answered Nov 24, 2020 in Node-js by Niroj
• 82,840 points
1,275 views
0 votes
0 answers

Remove double quotes from a Table Name using SEQUELIZE Nodejs

used Nodejs' SEQUELIZE to construct an Account ...READ MORE

Jun 27, 2022 in Node-js by Vaani
• 7,070 points
1,541 views
0 votes
1 answer

How nodejs get file name from absolute path?

Hello @kartik, Use the basename method of the path module: path.basename('/foo/bar/baz/asdf/quux.html') // returns 'quux.html' If you ...READ MORE

answered Jul 14, 2020 in Node-js by Niroj
• 82,840 points
3,425 views
0 votes
1 answer

Start script missing error when running npm start

It seems that there is an undefined ...READ MORE

answered Feb 10, 2022 in Java by Soham
• 9,710 points
4,438 views
0 votes
0 answers

Error: Can't set headers after they are sent to the client

I am using Node.js 4.10 and Express ...READ MORE

May 7, 2022 in Java-Script by narikkadan
• 63,600 points
1,400 views
0 votes
0 answers

TypeError: Router.use() requires middleware function but got a Object

There were some changes made in the ...READ MORE

May 12, 2022 in Java-Script by Kichu
• 19,040 points
7,812 views
0 votes
1 answer

Is there a way to download videos from YouTube Studio using NodeJS

Try this project in the github repository ...READ MORE

answered May 27, 2022 in Node-js by Neha
• 9,020 points
1,830 views
0 votes
1 answer

Can I display a TV channel from TV tuner card using NodeJS for an Electron APP?

Tvheadend is a streaming server for live ...READ MORE

answered Jun 10, 2022 in Node-js by Neha
• 9,020 points
784 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP