Downloading a media object but changing it to have a different file name

Let’s say I have a data table of media objects (containing jpg’s). I am displaying these in a repeating panel with a download icon. When the user hits download, I would like to download the file, but with a different name than what is stored in my data table (even maybe letting the user choose the name).

I thought there might be something simple like

anvil.media.download(self.item['image'], name = 'name.jpg')

However, the solution seems to be eluding me. I have also tried

self.image = self.item['image']
print(self.image.name)
self.image.name = 'name.jpg'
print(self.image.name)
anvil.media.download(self.image)

but both print's yield the same result (the name of the data table file). Then, I tried to play around with some of @stucork’s suggestions in Export table with Media column? but still couldn’t get anything working.

Does anyone have any quick solutions or hints or references?

Hi @gweber.lauraandleigh,

without having tested it, I’d try to costruct a new media object in memory and download it then.
Something like this maybe:

  orig =  self.item['image']
  new = anvil.BlobMedia('image/jpg',orig.get_bytes(),name='new_name.jpg')
  anvil.media.download(new)

I hope i was of any help (;

best,
Mark

3 Likes

Good stuff. That did the trick. Thanks!

1 Like