Using BlobMedia to return a file to the front end

I need to allow users to upload CSV files to anvil. Then the server will modify them & they will get a CSV response. I am able to upload & parse files but am stuck on the response.

I have gotten to the point where I am returning a text/plain BlobMedia object from the server to the front end. How do I make it automatically download? Or how to I expose it via button/link/etc?

If the BlobMedia is stored in a Data Table, its url property is a URL from which it can be downloaded. So you can set the url property of a Link to the url property of your BlobMedia, and clicking the Link will download the media.

If the BlobMedia is ‘anonymous’ (not stored permanently anywhere), you can still set the url of a Link object to the Media object itself to allow downloading it:

m = BlobMedia('text/plain', 'Hello, world!', name='hello.txt')
c = Link(text='Open text document', url=m)

Here’s a bit more info on uploading and downloading files.

Thank you Shaun. Can you clarify which of these steps happen on the front end or server side? If I have a CSV file on the server module, how would I get it to the UI?

Return it to the front-end as you’ve described, then set a Link’s url to the BlobMedia object.

I haven’t tried, but you might be able to create a link that links to the object following Shaun’s instructions, then start the download by calling its click event from your code to simulate the user clicking on it?

@shaun and all,

What if the link itself is already present and clicking it generates the data that you want to be downloaded?

I asked a similar question here:

Al

We’ve added a download function, so you can run this in a Form to trigger the browser to download a Media object:

download(my_media_obect)

Here’s an app that does that:

https://anvil.works/build#clone:4IMIYKI4QVGG3VEN=FLHJKXIOGXLL7WLLAFQWGDAR

1 Like