What I’m trying to do:
I want to transfer a set of different files selected by the user using Fileloader to an SQLite database located on my laptop. These file would be: an image file, a docx file, rich text file, plain text file. The files would be stored in the SQLite database as BLOBs.
Once saved in the database, I want to be able to then retrieve them in an Anvil App and save them to a separate files on my laptop. In the case of the image file and the rich text files, I also want to be able to display the image in an Anvil Image and also display the rich text data in a rich text box.
Its a hobby project. I’m just looking for pointers to a solution and in particular how in general I can exchange media files between an Anvil App on my laptop and python code on my server using the Uplink. I have no problem with using the uplink in general. I just can’t see an obvious path when using media files.
Upload the file using fileloader, and save to an Anvil data table
Retrieve the file as ‘media_object’, from the data table
You can get the bytes as media_object.get_bytes()
or:
import anvil.media
with anvil.media.TempFile(media_object) as file_name:
# Now there is a file in the filesystem containing the contents of media_object.
# The file_name variable is a string of its full path.
This article explains how to insert and retrieve binary files and images in sqlite:
Hi,
Thanks for the feedback.
I think I have the transfer from Server+SQLite working for .jpg, .docx, .txt working.
On the server side on my laptop connected to the SQLite db I retrieve the data from the database using the PYnative link you provide. Then I write it to a file as per the PYnative link.
Then I use the line media_object_photo = anvil.media.from_file(photoPath, “text/plain”) where photoPath is the location of the file on my laptop drive.
I then add media_object_photo to a dictionary of items I return to the Anvil app.
Its a bit clunky as I read the database item as a BLOB, store it to a file, read it back from the same file to convert it to an anvil.media, then transfer it back to the Anvil App world as a dictionary item when called by the Anvil App.
I haven’t as yet figured a solution for going the other direction from the Anvil app to my server code on my laptop. I will attempt the Anvil data table route as you suggest.