-
-
Notifications
You must be signed in to change notification settings - Fork 759
ui.download.from_url() opens url for download and ignores filename #4723
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Hi @morrowrasmus. Thank you for the report. Sorry but it is not NiceGUI issue.
https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/a#download Internally, NiceGUI spawns an nicegui/nicegui/static/nicegui.js Lines 255 to 270 in cd1906a
Workarounds could be (speculation but reasonable):
|
@evnchn Great analysis!
One could argue that it is an issue if we promise to trigger a download and provide a parameter called "filename":
If "download only works for same-origin URLs", maybe we need to warn or raise if the origin doesn't match? |
Fair point. I think, maybe it can be a combination of the two approaches:
|
Ok, what about the following proposal:
|
First point I agree, but second and third point I have hesitations about. I'm guessing your reasoning for a same-origin test on the client-side (creating the new But, I think passing Since, by all means the best practice is Meanwhile, I think it should be a warn, not an error, since:
|
I don't think we should enforce relative URLs. Take this ZIP for example, which works just fine: zip = 'https://github.com/zauberzeug/nicegui/archive/refs/heads/main.zip'
ui.button('Download').on_click(lambda: ui.download.from_url(zip)) By the way: The proposed warning should only be shown if the user defined a custom filename which will be ignored. |
Thanks for the explanation - we've ended up implementing a workaround to serve the filename we want that works well, so I hope you don't land on enforcing same-origin downloads. We are dependent on being able to serve external files in the application we are building as we are hosting the user-facing GUI one place and the file server another. Updated documentation and a warning if trying to set a custom filename when attempting to serve a cross-origin file would have been sufficient for me to understand the limitations here. |
If I am not mistaken, what's happening is, the browser is not abiding by your If you replace the But I don't think your argument and the warning quite connent, since if you do this code: zip = 'https://mysite.com/files/main.zip'
ui.button('Download').on_click(lambda: ui.download.from_url(zip, filename='mysite_main.zip')) Then this would work from So, if the goal is as simple as catching whether |
And, we should also have the relative URL check, if we pass in an URL, for which it is very obvious from the URL that it is downloading something which the browser can display and thus will not download but open in new tab. So, relative URL check is ran on:
|
In retrospect, I'd classify If that's the case, seeing the other 3 warning cases, I'm more inclined to always warning if the URL is not relative. Those who really want no warning can simply do They must use relative URLs for same-origin download though. That's non-negotiable because that's web development best practice. |
Uh oh!
There was an error while loading. Please reload this page.
First Check
Example Code
Description
Using
ui.download.from_url()
with thefilename
parameter specified, I expect that the downloaded is triggered directly (and not opened in a new tab first) and that the file will be served with whatever filename was passed. However, the file is opened in a new tab instead, and if this triggers a download, it retains the original name of the server.In the example found in the documentation, the same file is directly downloaded and not opened in a new tab. The example does not show the usage of the
filename
-parameter.NiceGUI Version
2.16.1.dev0
Python Version
Python 3.13.0
Browser
Chrome
Operating System
Windows
Additional Context
Tested on Chrome and Edge
The text was updated successfully, but these errors were encountered: