Debian Codegraph @

www/src/content/extensions/07-hx-download.md

raw symbols SCIP
1 ---
2 title: "hx-download"
3 description: "Save responses as file downloads with progress events"
4 category: "UX"
5 icon: "icon-[mdi--download-outline]"
6 keywords: ["download", "file", "save", "progress", "streaming"]
7 ---
8
9 The `download` extension saves a response as a file download instead of swapping it into the DOM. It streams the response body and fires progress events.
10
11 It activates in three ways:
12
13 - `hx-swap="download"` on the element
14 - The server responds with `Content-Disposition: attachment` (auto-detected, no attribute needed)
15 - The server responds with `HX-Download: <url>` (extension fetches that URL as the download, while the original response is still swapped normally)
16
17 ## Installing
18
19 ```html
20 <script src="https://cdn.jsdelivr.net/npm/htmx.org@__VERSION__/dist/htmx.min.js"></script>
21 <script src="https://cdn.jsdelivr.net/npm/htmx.org@__VERSION__/dist/ext/hx-download.js"></script>
22 ```
23
24 ## Usage
25
26 ### Explicit swap style
27
28 Use `hx-swap="download"` to always treat the response as a download:
29
30 ```html
31 <button hx-get="/files/report.pdf" hx-swap="download">
32 Download Report
33 </button>
34 ```
35
36 ### Auto-detect via Content-Disposition
37
38 If the server returns `Content-Disposition: attachment`, the extension triggers a download automatically — no `hx-swap` needed:
39
40 ```html
41 <button hx-get="/files/report.pdf" hx-target="#result">
42 Download Report
43 </button>
44 ```
45
46 ```http
47 Content-Disposition: attachment; filename="report.pdf"
48 ```
49
50 ### HX-Download header
51
52 When the backend cannot stream the file directly as the htmx response (e.g. it needs to redirect to a separate download endpoint), return an `HX-Download` header pointing to the file URL. The extension will fetch that URL as the download while htmx processes the original response body as a normal swap:
53
54 ```html
55 <button hx-get="/prepare-download" hx-target="#status">
56 Download Report
57 </button>
58 ```
59
60 ```http
61 HX-Download: /files/report.pdf
62 Content-Type: text/html
63
64 <span>Your download has started...</span>
65 ```
66
67 The `<span>` is swapped into `#status` and the file at `/files/report.pdf` is downloaded simultaneously.
68
69 This approach also avoids the indicator/disabled-element cleanup problem that occurs with `HX-Redirect` pointing to a download URL — indicators are cleared correctly after the original request completes.
70
71 ### Progress Bar
72
73 The extension fires events on the source element as the response streams in:
74
75 ```html
76 <button hx-get="/files/report.pdf" hx-swap="download">
77 Download Report
78 </button>
79 <progress id="prog" value="0" max="100"></progress>
80
81 <script>
82 document.body.addEventListener("htmx:download:progress", e => {
83 document.getElementById("prog").value = e.detail.percent
84 })
85 </script>
86 ```
87
88 The server must send a `Content-Length` header for percentage-based progress. Without it, `percent` will be `null` and only `loaded` (bytes received) is available.
89
90 ## Events
91
92 | Event | Detail | Description |
93 |---|---|---|
94 | `htmx:download:start` | `{total}` | Response headers received, streaming begins. `total` is `null` if no `Content-Length` |
95 | `htmx:download:progress` | `{loaded, total, percent}` | Fired for each chunk received. `percent` is `null` if no `Content-Length` |
96 | `htmx:download:complete` | `{filename, size}` | File download triggered in the browser |
97
98 ## Notes
99
100 - Binary files (PDFs, images, archives) are handled correctly.
101 - The filename is extracted from the `Content-Disposition` header (`filename` or `filename*=UTF-8''...`), falling back to the URL's last path segment.
102 - Cancellation works automatically via htmx's built-in request abort handling.
103 - For `HX-Download`, indicators and disabled elements are cleared correctly after the original request completes — the secondary fetch runs independently.