res.attachment()
Sets the Content-Disposition header to attachment to prompt a download.
Syntax
res.attachment([filename]) Parameters
| Name | Type | Required | Description |
|---|---|---|---|
filename | string | No | Suggested download filename; also sets Content-Type from its extension. |
Returns
Response — The response object, for chaining.
Examples
app.get('/report', (req, res) => {
res.attachment('report.csv');
res.send('id,name\n1,Ada\n');
});
Output
Content-Disposition: attachment; filename="report.csv"
Content-Type: text/csv; charset=utf-8
app.get('/blob', (req, res) => {
res.attachment();
res.send(buffer);
});
Output
Content-Disposition: attachment
Notes
Only sets headers; you still send the body yourself. For sending an
existing file from disk as a download use `res.download()`, which
also handles streaming and 404s.