Saving a media object from a data table to server

Hi,

ok first, I have spent hours trying to figure this out so believe me I don’t type here flippantly.

Last time I posted someone said I should demonstrate that I have searched for the answers so… just so you know I have read this: https://anvil.works/docs/media and these: https://anvil.works/docs/api/anvil#Media, https://anvil.works/docs/api/anvil#URLMedia, https://anvil.works/docs/api/anvil#BlobMedia

The information is obviously there I just need a little coaching to understand it.

My goal is to simply download a media file (image) from a data table to my server computer running at the office. yes there is a tutorial but it seems to show me how to save a text file and not how to save an image fie from a data table.

I would use the URL but as explained in a few posts, the url is only valid for the open session on the browser.

imagine there is a .jpg in the data table ( found like this: app_tables.product_background.get(theme_name=‘EPIC FRIDAY’)[‘theme_media’] )

… and you wanted to save to the server desktop with the name “test.jpg”

how would I do this?

thanks a million in advance! :slight_smile: :smiley:

Michael

If you are looking at feeding the image to an API request, then have a look at @meredydd’s example in this link :

Alternatively, if the local server is one you control, could you run an uplink script on it? That way you wouldn’t need to make an API call, you could just read the data table directly and return the media object.

Did I understand you problem correctly?

Hi David,

Thank you for your reply - yes that’s what I am trying to do. I do control the local server. Im not sure what EXACTLY “uplink script” means but I think I am doing that - I have a .py running on my local server that gets told what to do quite successfully by the anvil form.

I just don’t know HOW to download the media object from the data table.

This:
app_tables.product_background.get(theme_name=‘EPIC FRIDAY’)[‘theme_media’] )

Would provide the media from within the anvil form but how do I download to my local server as a file I can save to my desktop? The links I provided in my post appear to tell me how but I cannot fathom it. All they seem to do is save a text file.

This is my media: app_tables.product_background.get(theme_name=‘EPIC FRIDAY’)[‘theme_media’] )

What script do I put in my local server to download and save it?

Thanks a million in advance!

Michael

Ok, clone this demo :
https://anvil.works/build#clone:UVSH6MC5N2XOBXVH=4CKHRK6UQTQHF4FHXAR4B36Q

On your local server, run this :

pip install anvil-uplink

then create this script :

import anvil.server

# Connect to your anvil app.
anvil.server.connect("<your uplink key goes here")

# Call the function defined inside the Anvil IDE.
pic = anvil.server.call("fetch_pic")

# Open and write the returned media object to a binary file ("wb" for write binary)
with open("pic.jpeg", "wb") as fp:
    fp.write(pic.get_bytes())

You can download the file by clicking the button on the form, or you can download it from a python script running on your server with the uplink enabled.

3 Likes