[SOLVED] Loading media files via uplink

I’m loading data into an app from various spreadsheets and other local sources via uplink code. All working nicely so far.

However, I have a set of pdf files which need to be loaded into a data table (along with some metadata which lives in a spreadsheet) and I’m struggling to see how best to load them.

Do I need to create a form with a FileUploader or is there some way to load the media object directly from uplink code?

aha! I think I’ve found it. I incorrectly thought that URLMedia and BlobMedia weren’t available to me in uplink code but I just had the import wrong.

Given that I asked the question, I should probably give some form of answer for anyone that looks here.

Here’s the gist of what worked for me in my uplink code:

from pathlib import Path
import anvil.server
from anvil import BlobMedia
from tables import app_tables

anvil.server.connect(<uplink key>)

with Path(<path to directory>, <pdf file name>).open('rb') as file:
    media = BlobMedia(content_type='application/pdf', content=file.read(), name=<pdf file name>)
    app_tables.<table>.add_row(<media column>=media)
7 Likes

You just saved my ***, thanks!

1 Like