DEV Community

Cover image for How to download files from server to client using Node.js?
MELVIN GEORGE
MELVIN GEORGE

Posted on • Originally published at melvingeorge.me

How to download files from server to client using Node.js?

Originally posted here!

To download files from a Node.js server to a client, you have to read the file and set the response header Content-Disposition.

Let's say we have a PDF named resume.pdf in a directory called docs and we want to download that PDF whenever the user goes to /resume endpoint.

Let's write the code for that. 🦄

First create a read stream to read the file like this,

// filesystem module
const fs = require("fs");

// endpoint for /resume
app.get("/resume", (req, res) => {
  const rs = fs.createReadStream("./docs/resume.pdf");
});
Enter fullscreen mode Exit fullscreen mode

After that, set the Content-Disposition response header and give the value as attachment; filename=nameOftheFileYouWant.pdf. This tells the user's browser that this is an attachment that needs to download it.

// filesystem module
const fs = require("fs");

// endpoint for /resume
app.get("/resume", (req, res) => {
  // create read steam for the pdf
  const rs = fs.createReadStream("./docs/resume.pdf");

  // set response header: Content-Disposition
  res.setHeader("Content-Disposition", "attachment; john-resume.pdf");
});
Enter fullscreen mode Exit fullscreen mode

After setting the header all you have to do is pipe the read stream to the Response object res using the pipe() method like his,

// filesystem module
const fs = require("fs");

// endpoint for /resume
app.get("/resume", (req, res) => {
  // create read steam for the pdf
  const rs = fs.createReadStream("./docs/resume.pdf");

  // set response header: Content-Disposition
  res.setHeader("Content-Disposition", "attachment; john-resume.pdf");

  // pipe the read stream to the Response object
  rs.pipe(res);
});
Enter fullscreen mode Exit fullscreen mode

If you are using Express.js, you can use the res.download() method and pass the file path as the argument to it like this.

// filesystem module
const fs = require("fs");

// endpoint for /resume
app.get("/resume", (req, res) => {
  // express.js
  res.download("./docs/resume.pdf");
});
Enter fullscreen mode Exit fullscreen mode

Feel free to share if you found this useful 😃.


Top comments (0)