QA
Beginner LevelExpress JS

How does res.download() prompt user browsers to download files with custom attachment filenames?

❓ Interview Prompt: Explain res.download(), Content-Disposition attachment headers, file streaming from disk, and error handling callbacks.

'res.download(filePath, [filename], [callback])' transfers a file from disk to the client browser while forcing a file download prompt.

Header Setting:

Automatically sets the 'Content-Disposition: attachment; filename="custom-name.ext"' header, prompting the browser's download manager instead of displaying the file inline.

Code Implementation Example

javascript
import express from 'express';
import path from 'path';

const app = express();

app.get('/download-report', (req, res) => {
  const filePath = path.join(process.cwd(), 'storage/report-2026.pdf');
  
  // Force browser download with custom file attachment name
  res.download(filePath, 'Annual-Financial-Report-2026.pdf', (err) => {
    if (err) console.error('File download failed:', err);
  });
});
💡Prompting file downloads with custom attachment names using res.download.
🎯 Interview Key Takeaway

res.download() sets Content-Disposition: attachment headers to force client browser file download prompts.

#res.download#File Downloads#Headers#Beginner
Back to all Express JS Questions→

More Express JS Interview Questions

Beginner LevelExpress JS

What is Express.js and how does the middleware pipeline work?

Beginner LevelExpress JS

What is the difference between req.params, req.query, and req.body?

Beginner LevelExpress JS

How does express.Router() work for building modular route controllers?