How to download a file htmx way? #2741
Unanswered
wojciech-mazur-mlg
asked this question in
Q&A
Replies: 1 comment 2 replies
-
AFAIK, the common technique to trigger a file download without user interaction from javascript is by creating a dummy hidden link with a <a href="/generate-file" download
_="click me, on click remove me"> <!-- I don't know hyperscript's syntax, consider this pseudo code --> Instead of your current div, and use hyperscript to trigger a Clicking the link will trigger your browser's native file download popup with the progress bar showing, so I wouldn't worry about the lack of hx-indicator in this case as the browser itself will show progress for that download. Hope this helps! |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hello,
We are developing a web app using python FastAPI and htmx. We have a scenario of generating a file to download which seems to be quite common, but we don't know how to handle it correctly (in an interactive way) using htmx.
The scenario should look like this:
/download
endpointhx-indicator
)This is the desired scenario, but the biggest limitation is that htmx can't return a file, we need to issue "full" request to get a file.
Our current implementation is like this:
/download
endpoint/download
endpoint returns a partial html with toast message and this div:/generate-file
endpoint which should return a proper file using FastAPI StreamingResponse./generate-file
endpoint (but it's regular request with no HX-Request) and the file is generated and returned correctly....butIs there a proper way to handle such scenario? Do you have any ideas? Thank for any help.
Beta Was this translation helpful? Give feedback.
All reactions