Storing media in data tables (was Unexpected: Server code took too long)

If you go to add a column to a data table, you will see the option to add a Media column. This can hold any Anvil Media object - such as the file you get from a FileLoader. Eg the following works, assuming you have a data table called uploaded_files with a Media column called f:

  # On the client
  def fileloader_1_change(self, **params):
    anvil.server.call('save_file', self.file_loader_1.file)
# On the server
@anvil.server.callable
def save_file(file):
  app_tables.uploaded_files.add_row(f = file)

Bonus note:
It’s also easy to make media from data tables downloadable. Just get the url property of the Media object and set it as the URL of a Link component. This will get you a private download link for that file, which only works in that browser session. Eg:

  # On client
  def setup_download_link(self):
    # Get the first row from the uploaded_files table:
    r = app_tables.uploaded_files.search()[0]
    self.download_link.url = r['f'].url
    # Now you can download that file by clicking download_link
1 Like