Express.js res.attachment() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Response

Definition

In Express.js, res.attachment() sets the response Content-Disposition header to attachment. Browsers then treat the response as a download instead of showing it inline.

With a filename, Express also sets the filename= parameter and picks a Content-Type from the extension via res.type(). It only sets headers — you still send the body with res.send(), res.sendFile(), or similar.

Syntax

Call res.attachment in your Express.js app. Start with this line.

Express
res.attachment()
res.attachment(filename)
res.attachment();
res.attachment('path/to/logo.png');
CallWhat it does
res.attachment()Primary signature.
res.attachment(filename)Related call form.
res.attachment();Related call form.
res.attachment('path/to/logo.png');Related call form.

Install with npm install express. Create the app with const app = express() before you use this API in an Express.js tutorial project.

Library declarations
Express
const express = require("express");
const app = express();

res.attachment()
res.attachment(filename)
res.attachment();
res.attachment('path/to/logo.png');

Parameters

ParameterDescription
filenameRequired. Used for Content-Disposition filename= and for Content-Type via the extension (res.type()). Omit to set disposition only.

Return Value

res.attachment([filename]) returns res for chaining. It sets Content-Disposition (and related hints) but does not finish the response by itself.

Express
res.attachment('report.pdf')
  .sendFile(path.join(__dirname, 'files/report.pdf'));

Express.js Examples

Example 1 — Send Generated Text as a Download

Force a Save As dialog for a plain-text body you build in the route.

Express
const express = require('express');
const app = express();

app.get('/report', (req, res) => {
  res.attachment('report.txt');
  res.send('Daily report content');
});

app.listen(3000);

How It Works

  1. Mark as attachment. res.attachment('report.txt') sets Content-Disposition with that filename.
  2. Type from extension. .txt maps to text/plain through res.type().
  3. Then send the body. res.send delivers the string; headers were already set.
  4. Client saves the name. The download dialog typically suggests report.txt.

Example 2 — JSON Export with a Custom Name

Build JSON in memory and offer it as a named download.

Express
const express = require('express');
const app = express();

app.get('/export', (req, res) => {
  const payload = JSON.stringify(
    { ok: true, generatedAt: Date.now() },
    null,
    2
  );
  res.attachment('export.json');
  res.send(payload);
});

app.listen(3000);

How It Works

  1. Build the payload first. You already have the bytes — no disk file required.
  2. Name the download. export.json becomes the filename= value.
  3. Type follows .json. Express sets application/json from the extension.
  4. Send the string. Attachment headers tell the browser to download instead of rendering inline.

Example 3 — Attachment Header + res.sendFile()

Serve a real file from disk while forcing a download name. (For one-shot downloads, res.download is often simpler.)

Express
const express = require('express');
const path = require('path');
const app = express();

app.get('/guide', (req, res) => {
  res.attachment('user-guide.pdf');
  res.sendFile(path.join(__dirname, 'files', 'user-guide.pdf'));
});

// One-call alternative:
// res.download(path.join(__dirname, 'files', 'user-guide.pdf'), 'user-guide.pdf');

app.listen(3000);

How It Works

  1. Set download headers first. res.attachment('user-guide.pdf') marks the response as a PDF download.
  2. Stream the file next. res.sendFile reads the path and writes the body.
  3. Name can differ from disk. The attachment name is what the user sees; the path is where bytes come from.
  4. Shorter path: res.download(filePath, 'user-guide.pdf') does attachment + transfer in one call.

Runtime Support

res.attachment() is an Express.js response method. It runs on a Node.js server (Express 5 needs Node.js 18+). Browsers only receive the headers; they do not execute this method.

✓ Server · Express 4 & 5

res.attachment()

Works wherever Express runs: Node.js is the primary target. Bun and Deno can run many Express apps with compatibility layers. Not available in the browser.

Server Node.js runtime API
Node.js 18+ for Express 5 · Express 4 supported
Supported
Bun Express-compatible server apps
Supported
Deno Via npm compatibility / adapters
Supported
res.attachment() Express on Node.js

Bottom line: Study and run these examples with Node.js and the express package. Copy a snippet into a local file and start the server with node.

Frequently Asked Questions

It sets the Content-Disposition response header to attachment so browsers treat the response as a download. It does not read or send a file by itself.
Express sets Content-Disposition’s filename parameter from the base name, and sets Content-Type from the extension via res.type().
res.attachment() only sets headers. res.download() sets attachment headers and transfers a file from disk in one call.
Yes. Call res.attachment() first, then send the body with res.send(), res.sendFile(), or similar.
No. It is a stable Express response method in Express 4 and Express 5.

Next: res.clearCookie()

Expire a cookie by sending a past-dated Set-Cookie header.

res.clearCookie() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful