The HTML download Attribute vs. Content-Disposition: How Download Links Work

Compare the HTML download attribute with Content-Disposition, inspect file response headers, and test download behavior across browsers and origins.

Published
Reading time
7 min read
The HTML download Attribute vs. Content-Disposition: How Download Links Work
On this page

A link to a PDF, image, or other file can open it in the browser or prompt the visitor to save it. The outcome depends on more than the link: the HTML download attribute, the server’s Content-Disposition response header, the file’s origin, and browser settings can all matter.

This comparison explains what each mechanism controls, how they interact, and how to check the response your visitors actually receive.

Key Takeaways

  • The HTML download attribute asks the browser to download a linked resource and can suggest a filename. It does not give the page control over every cross-origin response.
  • The server can send Content-Disposition: attachment to request a download, including for a file hosted on another origin.
  • If the header and attribute suggest different filenames, browsers generally use the header’s filename. Conflicts between inline and the attribute can vary by browser.
  • Inspect the final response after redirects and test the browsers and devices your audience uses.

HTML download attribute vs Content-Disposition

HTML download attributeContent-Disposition header
Set byThe page author, on an <a> elementThe server or service returning the file
PurposeRequests a download and optionally suggests a filenameIndicates inline or attachment handling and can suggest a filename
Typical scopeSame-origin URLs, plus blob: and data: URLsThe response carrying the header, including a cross-origin response
Where to change itPage HTMLFile server, application, object storage, or CDN configuration

Neither mechanism guarantees an identical save dialog or behavior in every browser. User preferences, browser policies, sandboxing, and device capabilities also affect the result.

The relevant specifications are the HTML Living Standard’s download processing and RFC 6266 for HTTP Content-Disposition. This article could not be checked against live documentation or current browser releases; verify the behavior in your target environments.

Advertisement

Use the HTML download attribute

For a file on the same origin as the page, add download to the link:

<a href="/files/guide.pdf" download="field-guide.pdf">
  Download the field guide
</a>

Here, field-guide.pdf is a suggested filename, not a change to the file’s URL. If you omit the attribute’s value, the browser chooses a filename using available information, such as the response header or URL:

<a href="/files/guide.pdf" download>Download the guide</a>

An origin consists of a scheme, hostname, and port. A page at https://www.example.com and a file at https://files.example.com are cross-origin, even if the same organization owns both hosts. Do not assume the attribute will force a download from the second host. Browser handling is also affected by restrictions such as a sandboxed iframe’s permissions.

A link to a blob: or data: URL is another common use of the attribute, for example when a page creates a file in the browser. These URLs have their own lifetime and size considerations; they are not a substitute for configuring the server hosting a remote file.

Set Content-Disposition on the file response

When you control the server or storage service, send an attachment disposition with the file response:

Content-Type: application/pdf
Content-Disposition: attachment; filename="guide.pdf"

attachment asks the browser to download rather than display the response inline. Content-Type describes the representation; it does not by itself reliably force a download. Conversely, Content-Disposition: inline indicates that the response may be displayed in the browser if it can handle the content.

For a non-ASCII filename, a server can provide an ASCII fallback and a UTF-8 encoded filename* value:

Content-Disposition: attachment; filename="resume.pdf"; filename*=UTF-8''r%C3%A9sum%C3%A9.pdf

The intended Unicode filename is résumé.pdf. Clients that understand both parameters generally prefer filename*; older clients can use the fallback. Keep the actual header on one line and use a well-maintained header-building API rather than concatenating untrusted input. Treat filenames as suggestions: clients may sanitize or change characters that are unsafe on the local filesystem.

When both are present

If the server suggests server-guide.pdf in Content-Disposition while the link suggests field-guide.pdf, browsers generally use the header’s filename. Test any filename that matters to your workflow.

A response marked inline can also conflict with a link’s download attribute. Modern Chrome and Firefox generally prioritize the attribute for eligible downloads, while older browser versions and other environments may differ. Cross-origin restrictions still matter: do not rely on an attribute on your page to override a remote server’s response.

If you control neither the remote server nor its storage configuration, you may not be able to force its file to download. Host the file on infrastructure you control only if you have permission to do so, and configure its response there.

Advertisement

Inspect a file URL’s response headers

In a browser, open developer tools, select the Network panel, and navigate to or click the file link. Inspect the file request’s response headers for Content-Disposition and Content-Type. If there are redirects, inspect the final file response as well as the redirect chain. Browser developer-tool labels vary.

You can also make a GET request with curl:

curl -sS -L -D - -o /dev/null 'https://example.com/files/guide.pdf'

Replace the example URL with a file you are authorized to access. -L follows redirects, -D - prints response headers, and -o /dev/null discards the downloaded body. The output can contain multiple header blocks; check the final response’s status and headers. This command downloads the response body even though it discards it, so be mindful of large files and bandwidth costs.

A HEAD request may be useful for a quick check, but it is not a definitive substitute: a server or intermediary can handle HEAD differently from the GET request used to retrieve the file. Also test authenticated or signed URLs in the same context visitors use, without exposing credentials in shared logs.

Cross-browser testing checklist

Use a small test page and files you control. Record the browser, version, operating system, and relevant settings for each result.

  1. Test a same-origin link with download and a suggested filename. Check whether the file saves and which name appears.
  2. Repeat with Content-Disposition: attachment, first without the attribute and then with a conflicting filename in the attribute.
  3. Test an inline response with the attribute, then without it. Note whether the file opens or downloads.
  4. Test a cross-origin file, with and without an attachment header on its response. Include any redirect to a different origin.
  5. Repeat in the desktop and mobile browsers your audience uses. If your page runs inside a sandboxed iframe, test that deployment too.

Check the actual saved filename, not just whether a dialog appeared. A browser configured to save automatically may download without showing a prompt.

Common Mistakes

  • Assuming a different subdomain is same-origin. Hostname differences make it cross-origin, as do scheme or port differences.
  • Inspecting only the page response. The disposition must be on the file response, not merely on the HTML page containing the link.
  • Checking only the first redirect. The final response may come from a different host or have different headers.
  • Expecting download to override every server response. Origin restrictions and browser behavior can prevent that.
  • Testing only with curl. Curl reveals HTTP responses, but it does not reproduce a browser’s download UI, policies, or settings.

Best Practices

  • Use Content-Disposition: attachment when you control the file response and want download-oriented handling across links and clients. Add the HTML attribute when it is useful for an eligible link, but test the combination.
  • Set an accurate Content-Type and provide a sensible, safe filename. Avoid inserting unsanitized user input into response headers.
  • Check CDN, storage, and cache configuration if a header change does not reach visitors; cached responses can retain earlier headers.
  • Treat download handling separately from access control. A download attribute or disposition header does not authenticate users or make a file private.

Conclusion

Use the HTML attribute to express download intent from a page, and configure Content-Disposition when you control the file response. Inspect the final response and test the actual browsers and origins involved before relying on either behavior.

What would your domain sell for?

A free TidyURLs account unlocks the Domain Estimator. It prices a name from 191,011 real domain sales.