Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Download a file from NodeJS Server using Express

How can I download a file that is in my server to my machine accessing a page in a nodeJS server?

I'm using the ExpressJS and I've been trying this:

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?

like image 295
Thiago Miranda de Oliveira Avatar asked Sep 02 '11 20:09

Thiago Miranda de Oliveira


People also ask

How do I download Node.js Express?

Installing Express Use the following command to install express: npm install express --save.

How does res download work?

The res. download() function transfers the file at path as an 'attachment'. Typically, browsers will prompt the user to download. Parameters: The filename is the name of the file which is to be downloaded as attachment and fn is a callback function.


Video Answer


2 Answers

Update

Express has a helper for this to make life easier.

app.get('/download', function(req, res){   const file = `${__dirname}/upload-folder/dramaticpenguin.MOV`;   res.download(file); // Set disposition and send it. }); 

Old Answer

As far as your browser is concerned, the file's name is just 'download', so you need to give it more info 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 can set the header value to whatever you like. In this case, I am using a mime-type library - node-mime, to check what the mime-type of the file is.

Another important thing to note here is that I have changed your code to use a readStream. This is a much better way to do things because using any method with 'Sync' in the name is frowned upon because node is meant to be asynchronous.

like image 142
loganfsmyth Avatar answered Sep 29 '22 12:09

loganfsmyth


Use res.download()

It transfers the file at path as an “attachment”. For instance:

var express = require('express'); var router = express.Router();  // ...  router.get('/:id/download', function (req, res, next) {     var filePath = "/my/file/path/..."; // Or format the path using the `id` rest param     var fileName = "report.pdf"; // The default name the browser will use      res.download(filePath, fileName);     }); 
  • Read more about res.download()
like image 31
Jossef Harush Kadouri Avatar answered Sep 29 '22 13:09

Jossef Harush Kadouri