Question about manipulating files on the Server Side

What I’m trying to do:
I have Excel files stored in a data table. From the client I pass a server function the row ID in that table. On the Server Side I am trying to re-use code that processes a file (previously) passed from a file loader. When the FileLoader passes the file, the server identifies the type as <class ‘anvil.FileMedia’>. When the server code retrieves the file from a data table, it is identifying it as <class ‘anvil._server.LazyMedia’>

What I’ve tried and what’s not working:
I’ve looked at how to ‘convert’ that, but no dice. Ultimately what I need to do is read that file into a dataframe.

You’re probably looking for the get_bytes() member function, seen here:

It returns the contents of the file.

Thanks Phil, so I’ve tried several uses of get_bytes() to no avail. This is what I know:

From my client code when I call my server function with a file from a file loader, the server function shows me that the file type is anvil.FileMedia. With this, my server code receiving the file as a parameter works fine:

  with anvil.media.TempFile(file) as file_name:
    if file.content_type == 'text/csv':

however, different client code (trying to use the same server code) sending a file retrieved from a table is sending ‘file’ as LazyMedia to that server function. That… is not working. I wonder how on the client side I can retrieve that media object from my data table, but provide it as a parameter to the server function such that it’s also received as an anvil.FileMedia object?

What is type(file), in each case? We’re not seeing the code that fills in the value of file, and I’d prefer to avoid trying to guess the type.

When I send the file from the FileLoader, it’s of type:

<class ‘anvil.FileMedia’>

When I retrieve the file from the table, and send that… it’s of type:

anvil._server.LazyMedia

Each of these is a subclass of anvil.media. So if I read the docs correctly, you should be able to call get_bytes() on that object, directly. No need to convert it into something else, first.

Not sure if it is relevant but perhpas a bug crept back in related to lazy media. But yeah, get_bytes() should be the answer (assuming there are bytes there).

Thanks folks - This worked, where manifest is a media object (Excel file) of type anvil._server.LazyMedia, retrieved from a data table:

    file = anvil.BlobMedia(content_type="bytes", content=manifest.get_bytes())

Thanks for the push start