Lifespan of a temporary server file

In another post, I was told this regarding storing files in my user space on the server :

I need to receive a file which could be 500k lines or even more, store it, analyse it and get some user feedback (I will be asking the user to select which column relates to which field in the DB based on the displayed sample data). Only when this interaction is complete do I want to insert it into the database.

Ordinarily, I would store the file in a temp directory, read the first 10 lines, display them in my interaction form then process as required after that. But it looks like I can’t rely on the file still being there.

My two thoughts were (1) to create a temporary table with a TEXT field and dump it all in there and reference that, or (2) read the first x lines, dump the file, then re-upload it once the mapping was established. Neither seem brilliant, though (2) i think might be easier.

I was wondering if there was a better way?

There is! Anvil now supports storing Media in data tables, so you can store it there. Just create a Media column in your table, and save the uploaded file into there. Eg:

def file_loader_1_change(self, files, **params):
  # Store the file in data tables
  self.row['file'] = self.file_loader_1.file

As this is a new feature, everyone is starting with a 10MB quota. If you need more storage space than this, drop us a line at contact@anvil.works.

(Expect a formal announcement of this feature in the next few days)

1 Like