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 in Node-js by Vaani
• 7,020 points
105 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 by Neha
• 8,740 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,740 points
14,521 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,740 points
459 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 in Node-js by Vaani
• 7,020 points
58 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,740 points
2,486 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 in Java by Soham
• 8,740 points
2,225 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 in Java-Script by narikkadan
• 11,280 points
259 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 in Java-Script by Kichu
• 19,040 points
1,405 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 in Node-js by Neha
• 8,740 points
128 views
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP