I give more information about the problem. The bug only happens when:
1 - I click on Run (my app is not published yet).
2 - I go to the page where are the “buttons” with the links to the files that are stored in the database as media.
3 - I click on the Download button (It works correctly, I can download the file in my browser).
4 - I click on close to close the application.
5 - I click on Run again.
6 - I repeat step 2
7 - I click on the Dowload button (now it doesn’t work anymore, I get the error message: “Bad request: Invalid (Lazy) Media object” (I use Firefox browser).
PS: If I close Anvil, close the browser and repeat steps 1, 2 and 3 it works correctly, the problem is when I close the application, return to the console, and restart the application, which is when the download button no longer works.
I have checked the URL of the button and I have realized that when I restart the application from the console, the URL of the buttons change and no longer work.
Blockquote
Form1:
import anvil.media
def button_load_file_links_click(self, **event_args):
results = anvil.server.call('get_filenames_list_from_data_table').
if results is not None:
self.repeating_panel_1.items = results.
RowTemplate1:
from ._anvil_designer import RowTemplate1Template
from anvil import *
import anvil.server
import anvil.tables as tables
import anvil.tables.query as q
from anvil.tables import app_tables
class RowTemplate1(RowTemplate1Template):
def __init__(self, **properties):
# Set Form properties and Data Bindings.
self.init_components(**properties)
# Any code you write here will run when the form opens.
self.link_file.url = self.item['file_object'].url
I can confirm that this was a problem with an app I did not change for 8+ months. It was about 2 weeks ago I had the problem.
It similarly used links that got the url property from a media object that was stored in a data table (retrieved from a server module function call).
I can also confirm that this seemed to only be reproducible using Firefox and the new MS Edge browser, but that I could not reproduce it on-demand, only every few times in a row I tried.
The only problem now is I cannot reproduce the issue again at all. After trying to fix it for several days, it seemed to just go away. My users were able to take the url link in the browser bar that reported the error “Bad request: Invalid (Lazy) Media object” and hit enter and run it again and it would then download the file.
This made me think it was a race condition (that the on-the-fly link generated was not yet ‘connected’ to the information), leading me to set all sorts of timers and sleeps etc, but the error still seemed to only reproduce randomly.
File handling is not my strong point but I have encountered a similar problem. If I understand your issue correctly you it may be because you are using a ‘private’ link to the datatable media which is only valid for a session. Have a look at this section from the Docs:
url
Gives you a URL where this media can be downloaded, if this Media is “permanent” (e.g. if it is stored in Data Tables, or a Google Drive file). If a Media object is not permanent (e.g. it is an anonymous BlobMedia object), its url property will be None. However, you can still download non-permanent Media objects using Link components, or display them with Image components.
Some media’s URLs are accessible from anywhere on the web (e.g. URLMedia('https://anvil.works/')).But some media objects provide a special URL, which is only valid for this browser session using this app. For example, the url of media from a data table is only valid in the session when it was generated.
So if you created the url and saved it to the datatable, the url you created was only valid for that session. Which is why the link doesn’t work the second time.
You would probably be better off fetching a copy of the Media from the datatable or using an HTTP endpoint.
I removed the media.url property being passed from my old code to a link object as well and it still works passing the media object directly, thanks @jshaffstall