Intermediate LevelREST APIs & Web Services
How does Content-Disposition header control file download filenames and inline viewing?
❓ Interview Prompt: Explain `Content-Disposition` header, `inline` vs `attachment`, specifying file download filenames (`filename="report.pdf"`), and security.
The 'Content-Disposition' HTTP response header tells the browser whether to display a file inline or prompt a file download dialog:
Header Variants:
1
'Content-Disposition: inline': Browser displays the file inside the browser window (e.g. viewing PDF or image directly).
2
'Content-Disposition: attachment; filename="Monthly_Report_2026.pdf"': Forces browser to open "Save As" download dialog with the designated filename!
Code Implementation Example
typescript
app.get('/api/v1/reports/pdf', (req, res) => {
const filePath = './reports/july.pdf';
res.setHeader('Content-Type', 'application/pdf');
res.setHeader('Content-Disposition', 'attachment; filename="Monthly_Report_2026.pdf"');
fs.createReadStream(filePath).pipe(res);
});💡Setting Content-Disposition attachment header to force PDF file downloads.
🎯 Interview Key Takeaway
Use Content-Disposition: attachment; filename=... to force browser file downloads with custom filenames.
#Content-Disposition#Downloads#HTTP Headers#Intermediate
Back to all REST APIs & Web Services Questions→