Content-Disposition
File downloads and inline rendering are controlled by the Content-Disposition response header, which indicates whether the browser displays content directly or triggers a download dialog.
Usage
In a standard HTTP response, the Content-Disposition header
controls how the browser presents content to the user. Setting
the value to inline tells the browser to render the content
directly. Setting the value to attachment triggers a download
dialog instead.
This header also plays a role in
multipart/form-data responses and form submissions. Each part
of a multipart body includes its own Content-Disposition
field, identifying the form field name and optional filename
for uploaded files. The part boundary is defined in the
Content-Type header.
Directives
inline
The inline directive instructs the browser to display the
content directly within the page. This is the default behavior
for most responses when Content-Disposition is absent.
attachment
The attachment directive triggers a download prompt. The
browser presents a "Save As" dialog rather than rendering the
content in the viewport.
filename
The filename parameter suggests a default filename for the
downloaded file. The value is enclosed in double quotes and
follows ASCII encoding rules.
Content-Disposition: attachment; filename="report.pdf"
filename*
The filename* parameter extends filename with support for
character encoding beyond ASCII. When both filename and
filename* are present, filename* takes
priority. This is useful for filenames containing non-ASCII
characters.
Content-Disposition: attachment; filename="document.pdf"; filename*=UTF-8''d%C3%B6cument.pdf
form-data
The form-data directive appears in multipart body parts and
identifies the content as belonging to a form submission.
name
The name parameter is required within multipart body parts
and identifies the form field the part corresponds to.
Example
A server responding with a CSV file download sets
attachment and provides a suggested filename. The browser
opens a save dialog with "export.csv" pre-filled.
Content-Disposition: attachment; filename="export.csv"
A server displaying a PDF directly in the browser uses
inline. The browser renders the PDF in its built-in viewer
rather than downloading the file.
Content-Disposition: inline
In a multipart form submission, each part carries its own
Content-Disposition field. The name parameter maps the
part to the corresponding form field, and filename indicates
the original name of an uploaded file.
Content-Disposition: form-data; name="avatar"; filename="photo.jpg"
Troubleshooting
Download and display problems with Content-Disposition typically stem from incorrect directive values or filename encoding mistakes.
File downloads with the wrong filename. The browser uses the
filenameparameter as the default save name. A missing or malformedfilenamevalue causes the browser to fall back to the URL path or a generic name. Ensure the value is enclosed in double quotes:filename="report.pdf". Verify the header withcurl -I https://example.re/fileand check thefilenameparameter in the response.Non-ASCII characters in filenames break or display as garbage. The
filenameparameter supports only ASCII characters. For names with accented letters, CJK characters, or other non-ASCII text, add thefilename*parameter using the encoding syntax:filename*=UTF-8''d%C3%B6cument.pdf. Include bothfilename(ASCII fallback) andfilename*(encoded version) for maximum compatibility across browsers.Browser ignores attachment and displays content inline. Some browsers override
attachmentfor content types they render natively, such as PDFs and images. The Content-Type header influences this behavior. SettingContent-Type: application/octet-streamalongsideattachmentforces a download for any file type. Browser extensions and built-in viewers (PDF.js) can also override the directive.Inline display triggered when download is expected. A missing Content-Disposition header defaults to
inlinefor most content types. The server must explicitly sendattachmentto trigger a download. In nginx, addadd_header Content-Disposition "attachment";to the relevantlocationblock. In Apache, useHeader set Content-Disposition "attachment"within a<Location>or<FilesMatch>directive.filename vs filename precedence.* When both parameters are present,
filename*takes priority in all modern browsers. Older clients that do not supportfilename*fall back tofilename. Always include both parameters. Thefilename*value must use percent-encoding for special characters:filename*=UTF-8''na%C3%AFve.txt.Framework-specific download response helpers. Most web frameworks provide built-in methods for setting download headers. Django uses
FileResponsewith theas_attachmentparameter. Express usesres.download(). Flask usessend_file()withas_attachment=True. Rails usessend_dataorsend_filewith adispositionoption. These methods handle filename encoding and header formatting, reducing the chance of manual errors.