I have an Anvil website with a database to which I have uploaded data files and info about those files.
I am using a client program to download, using the API endpoint, info about the files.
I now want to know how to set up the API to receive the actual file from the Anvil website.
I’d be grateful if someone could point me in the right direction.
thanks
If your “client program” is an actual Anvil App, then this portion of the Documentation may be of use:
Uploading and downloading files
1 Like
Thank you, but my client is a separate python program.
If I recall correctly, in your HTTP endpoint just return the media object that you fetched from your data table. Anvil takes care of setting the headers based on the type of file it is.
Is it an Uplink program? If so, then it can use some Server-side features to retrieve the Media Object.
Thank you for your suggestions. I finally got there. In case anyone else is looking for similar help, this is how i did it.
The row in the table has a field for the ‘filename’ and a field for the content ‘file’:
The server API handler was as follows:
@anvil.server.http_endpoint("/campaign_file/:filename")
def campaign_file(filename):
campaign = app_tables.campaigns.search(filename=filename)
return campaign[0]['file']
And the code in my client was:
def download_campaign(self, filename) -> None:
result = requests.get(
f"MY_ANVIL_PATH/_/api/campaign_file/{filename}"
)
campaign = result.content
if result.status_code == 200:
with open(f"{filename}", "wb") as downloaded_file:
downloaded_file.write(campaign)
I think the problem I was having was that it took me a while to realise that although the search returned a single item, it was still presented as a list of one,
[Of course it still needs error checking etc.]
This now seems to work.
Thanks for all you suggestions.
If you know that the search will return a single item, you can use get instead.
1 Like
Thank you, I will note that for the future.