brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: the download attribute on a link suggests the filename, with a same-origin catch

Web-Development
~/tips/tip-html-a-download-filename

cat ~/tips/tip-html-a-download-filename.md

Makes a link save the file with a name you suggest, only for same-origin URLs.

A link to a PDF or an image often opens in the browser instead of saving. The download attribute asks the browser to save it instead, and it can suggest a better filename than the one in the URL. My advice is to treat it as a hint and set the real name on the server too.

With no value, the browser picks a name. With a value, that is the suggested name:

<a href="/files/report-2026.pdf" download>Download report</a>
<a href="/files/a3f9c1.pdf" download="invoice-november.pdf">Download invoice</a>

It also works with links you build in JavaScript, which is how you let someone save generated text or an image from a canvas:

const blob = new Blob(["hello"], { type: "text/plain" });
const a = document.createElement("a");
a.href = URL.createObjectURL(blob);
a.download = "hello.txt";
a.click();
URL.revokeObjectURL(a.href);

The catch is that download only works for same-origin URLs, same-origin blob: URLs and data: URLs. For a link to another site the attribute is ignored and the browser just navigates there. So you cannot force a download of someone else’s file with it.

More caveats:

  • If the server sends a Content-Disposition header with a filename, that name wins over the attribute.
  • The browser may still change the name to fit the file system, and slashes are replaced with underscores.
  • What happens next depends on the browser and the user’s settings. It may ask, save straight to a folder, or open the file in an app.

Treat the filename as a suggestion and the attribute as a nicety, not as a security feature.

Further reading: MDN: <a> download attribute

More Web-Development tips ab -n 200 -c 10 https://example.com/ console.assert(items.length > 0, 'cart is empty', items) document.designMode = "on" function save(item) { Save draft All Web-Development tips (67) All topics

Link