Why your PDF downloads instead of opening, and how Content-Disposition decides
You link to a PDF. In one browser it opens in the viewer, in another it lands in the downloads folder. Nothing about the file changed. What differs is two response headers, Content-Type and Content-Disposition, and which of them the browser believes.
The two headers and the division of labour
Content-Type says what the bytes are. Content-Disposition says what to do with them.
Content-Type: application/pdf
Content-Disposition: inline; filename="invoice-2026-11.pdf"
inline means render it here if you can. attachment means save it, do not render it. Leave Content-Disposition off entirely and the browser decides for itself, which is where the inconsistency comes from: a browser with a built in PDF viewer displays it, one without downloads it, and a browser configured by the user to always download does that instead.
So the first rule is boring and effective: if you care which happens, say so. An absent header is not a neutral default, it is a request for the browser's opinion.
When the headers disagree
The confusing cases are the ones where the two headers pull in different directions.
Content-Type: application/octet-stream with Content-Disposition: inline downloads anyway. octet-stream means unspecified bytes, and there is nothing to render. This is the most common cause of a file that stubbornly downloads despite an inline header: the server never worked out the real type. Object storage is a frequent culprit, because an upload with no type set is stored and served as octet-stream forever.
Content-Type: application/pdf with Content-Disposition: attachment downloads, correctly. attachment wins over a renderable type, which is the point of it.
Content-Type: text/html with Content-Disposition: attachment downloads, and that combination is a deliberate security control rather than an inconvenience. More on that below.
Filenames, and the part that breaks on accents
The filename parameter suggests what to call the saved file. Plain filename="report.pdf" is limited to ASCII, so a name with an accent, a Chinese character, or an emoji cannot be expressed in it.
The fix is the filename* form, which carries an encoding:
Content-Disposition: attachment; filename="rapport.pdf"; filename*=UTF-8''rapport-financi%C3%A8re.pdf
Send both. Clients that understand filename* use it and get the correct name; anything older falls back to the ASCII filename. The value after UTF-8'' is percent encoded, and the two single quotes are required syntax rather than a typo.
Two things to get right. Strip any path separators and control characters from a user supplied name before it reaches the header, because a filename is attacker controlled data in most applications. And remember a newline in that value is a header injection, so it has to be rejected rather than escaped.
Making a download actually download
The reliable combination:
Content-Type: application/pdf
Content-Disposition: attachment; filename="invoice.pdf"; filename*=UTF-8''invoice.pdf
If a file still opens rather than downloads after setting that, check what the server is really sending rather than what the code says it sends. A CDN, a proxy, or a storage layer may be rewriting or dropping headers in between. The HTTP headers tool shows the response headers a host actually returns for a URL, which settles that question quickly.
On the other side, forcing a download when you wanted a preview usually means the type is wrong rather than the disposition. Look up what the extension should map to with the MIME types reference, then set it explicitly at the point of upload rather than relying on the storage layer to guess.
Why attachment is a security control
Serving user uploaded files from a domain that also runs your application is how stored cross site scripting happens. A user uploads a file that is really HTML, another user opens it, and the script runs with the privileges of your origin: same cookies, same session, same local storage.
Content-Disposition: attachment blocks the rendering path. The file is saved rather than parsed, so nothing executes in your origin. Pair it with:
Content-Type: application/octet-stream
Content-Disposition: attachment; filename="upload.bin"
X-Content-Type-Options: nosniff
nosniff matters here. Without it, a browser that distrusts your declared type may inspect the bytes and decide the file is HTML after all, which undoes the protection. With it, the browser is required to honour the type you sent. The security headers check will tell you whether nosniff is present across a site.
The stronger measure is serving user content from a separate origin entirely, so that even a rendered file has no access to your application's cookies. Disposition and nosniff are the defence in depth behind that, not a replacement for it.
Range requests and resumable downloads
One more header is involved in large downloads. If a download of a large file cannot be resumed after a dropped connection, the server is probably not advertising range support:
Accept-Ranges: bytes
Without it, a client that loses the connection at 90 percent starts again from zero. Most static file servers set this; many application endpoints that stream a file out of a database or an object store do not, because they were written as a single read to the response body. Worth checking on any endpoint that serves files above a few megabytes. The HTTP status codes reference covers 206 Partial Content and the responses a ranged request produces.
A short checklist
- Set
Content-Typeaccurately at upload time, not at download time. - Set
Content-Dispositionexplicitly whenever the behaviour matters. Absent is not neutral. - Send
filenameandfilename*together for anything that may be non ASCII. - Sanitise user supplied filenames before they reach the header.
- Use
attachmentplusnosnifffor user uploaded content, and a separate origin if you can. - Check the headers on the wire, not in the code, when the behaviour surprises you.