Express.js res.attachment() Function

Last Updated : 24 Sep, 2026

The res.attachment() function sets the Content-Disposition HTTP response header to attachment, indicating that the response should be treated as a downloadable file. When a filename is provided, Express also sets the Content-Type based on the file extension and adds the filename to the Content-Disposition header.

frame_3476

Syntax: 

res.attachment( [filename] )

Parameter: The filename parameter is optional and specifies the filename to include in the Content-Disposition header.

Return Value: Returns the response object (res).

Installation of the Express module

You can install this package by using this command. 

npm install express

After installing the express module, you can check your express version in the command prompt using the command. 

npm version express

After that, you can just create a folder and add a file, for example, index.js. To run this file you need to run the following command. 

node index.js

Project Structure:

NodeProj

Example 1: Setting Content-Disposition

javascript
const express = require('express');
const app = express();
const PORT = 3000;
app.get('/', (req, res) => {
    res.attachment('Hello.txt');
    console.log(res.get('Content-Disposition'));
    console.log(res.get('Content-Type'));
    res.send('Response attachment header set successfully.');
});
app.listen(PORT, () => {
    console.log(`Server listening on PORT ${PORT}`);
});

Steps to run the program: 

Run the index.js file using the below command: 

node index.js

Output:

Console Output:  

Server listening on PORT 3000

Browser Output: 

Open your browser and go to http://localhost:3000/, now you can see the following output on your browser: 

Screenshot-2026-08-17-102652

Console Output:

Screenshot-2026-08-17-102545

Example 2: Using res.attachment() Without a Filename

JavaScript
const express = require('express');
const app = express();
const PORT = 3000;
app.get('/', (req, res) => {
    res.attachment();
    console.log(res.get('Content-Disposition'));
    res.send('Attachment header set without a filename.');
});
app.listen(PORT, () => {
    console.log(`Server listening on PORT ${PORT}`);
});

Steps to run the program: 

Run the index.js file using the below command: 

node index.js

Output:

Console Output:  

Server listening on PORT 3000

Browser Output: 

Open your browser and go to http://localhost:3000/, now you can see the following output on your browser: 

Screenshot-2026-08-17-103159


Console Output:

Screenshot-2026-08-17-103010

Working of res.attachment()

  • An Express application receives a client request.
  • The route handler gets access to the response object through res.
  • res.attachment() sets the Content-Disposition response header to attachment.
  • When a filename is provided, Express adds it to the filename parameter.
  • Express also determines the Content-Type from the filename extension.
  • The response can then be sent using methods such as res.send() or res.sendFile().

Use Cases of res.attachment()

  • Setting a response to be treated as a downloadable attachment.
  • Setting a filename for a file response.
  • Specifying the appropriate content type based on a file extension.
  • Preparing response headers before sending a file.
  • Controlling how browsers handle file responses.
Comment