Display image from uploaded file temporary cache

Lets say I have a standard file uploader, and a simple image below it, I want to be able to display the image on page as soon as it is uploaded. Have tried every iteration I can think of by setting image.source to methods of Media Object. I assumed there has be a way to display an image from cache vs uploading and storing the image permanently. I have figured out how to store the file to table, and then display image from table, but am looking to display the image from immediate upload cache not permanent location. Is this functionality possible or am i missing something super simple?
Simple Example

class Form1(Form1Template):

      def __init__(self, **properties):
        # You must call self.init_components() before doing anything else in this function
        self.init_components(**properties)

      def file_loader_1_change (self, files, **event_args):
        # This method is called when a new file is loaded into this FileLoader

        orig_pic = self.file_loader_1.file
        self.image_1.source = orig_pic  #have tried many different iterations, orig_pic.url, etc.
        self.image_1.visible = True
1 Like

Hi, and welcome to the forum. That code is entirely correct! It also works for me:

https://anvil.works/ide#clone:GK2LSQ2IJ4XBBFRZ=ETFF6KNCCQRYDQJGHOAWDDR5

The only thing I can think of - is the file_loader_1_change method running at all? It needs to be bound to the change event on the file_loader_1 component. (This should have happened automatically when you double-clicked on the component, but perhaps you accidentally changed something while you were fiddling around trying to get it working.)

To check, select that component and scroll to the bottom of the Properties panel. Make sure that the event handler for the change event is filled out correctly. Here’s how it looks for my file_loader_1 component:

16

Thanks, I just went back and tested the code and it is now working!

2 posts were split to a new topic: Displaying an image by URL