You are currently viewing the new Anvil Editor Docs.
Switch to the Classic Editor Docs
You are currently viewing the Classic Editor Docs.
Switch to the new Anvil Editor Docs

Files, Media and Binary Data

All binary data (pictures, uploaded files, etc.) is represented in Anvil as Media objects. Check the Quickstart to see some examples using them.

Media Objects

Media objects are created by Anvil APIs such as FileLoader components and the Google Drive API. You can also create them directly in Python code from byte strings or source URLs.

You can pass Media objects to and from server functions, store them in Data Tables, and use them with Anvil components.

Here are a few things you can do with Media objects:

  • Display a picture in an Image component by setting its source property to a Media object.
  • When a user uploads a file into a FileLoader component, the FileLoader’s file property is a Media object.
  • You can send and receive email attachments as Media objects.
  • You can open any Media object by setting the url property of a Link to a Media object. This opens or downloads the media when the link is clicked.
  • You can trigger a Media object download in the user’s browser by calling in any client-side code. (Remember to import first!)
  • You can store media in a Data Table, with the “Media” column type.
  • Canvas components can take a snapshot of what you’ve drawn as a Media object by calling get_image().
  • Google Drive files are Media objects. You can set the contents of a new Google Drive by passing a Media object to create_file(), or you can upload new contents to an existing file by calling set_media().
  • You can create a PDF file from any component or Form.

You can do all these things without needing to manually manipulate the binary data.

If you do need to access the underlying data and metadata, you can use the attributes and methods of the anvil.Media class listed below.

Using Media Objects directly

API Docs: anvil.Media | anvil.URLMedia | anvil.BlobMedia

All Media objects are subclasses of anvil.Media and all have these attributes and methods:


Tells you the MIME type of this media. This is a string with values like "text/plain" or "image/png".


A number that tells you how many bytes long this media is.


A string containing the filename of this Media object (or None if it is anonymous)


Gives you a URL where this media can be downloaded, if this Media is “permanent” (e.g. if it is stored in Data Tables, or a Google Drive file). If a Media object is not permanent (e.g. it is an anonymous BlobMedia object), its url property will be None. However, you can still download non-permanent Media objects using Link components, or display them with Image components.

Some media’s URLs are accessible from anywhere on the web (e.g. URLMedia('')).

But some media objects provide a special URL, which is only valid for this browser session using this app. For example, the url of media from a data table is only valid in the session when it was generated.


A method that returns the content as a string. For binary content such as images, this isn’t very pretty to print out, but it gives you direct access to the raw content of this Media.


A method that returns the url property or None if the Media object doesn’t have one. By default, the returned URL will auto-download the file. You can pass False as an argument to get_url to return the non-auto-downloading version of the URL. The file will instead be opened by the browser.


This example gets the website and prints the data and metadata made available by its Media object.

my_media = anvil.URLMedia('')

print(f'url: {my_media.url}')
print(f'content_type: {my_media.content_type}')
print(f'length: {my_media.length} bytes')
# This will be `None` since this is a website, not a file
print(f'name: {}')
# Only print the first 15 bytes
print(f'raw bytes: {my_media.get_bytes()[:15]} ...')

This prints:

content_type: text/html
length: 38653 bytes
name: None
raw bytes: <!DOCTYPE html> ...

Constructing Media Objects

As well as getting Media objects from FileLoaders, Google Drive and other Anvil APIs, you can construct them yourself in code.

URLMedia gets media from a URL. This example gets media for the Anvil logo, and displays it in an Image component called image_1.

my_media = anvil.URLMedia("")

self.image_1.source = my_media

URLMedia only stores a URL; it does not fetch its data unless the length or content_type attributes are accessed, or get_bytes() is called.

Because of browser security restrictions, it is often not possible to directly grab the bytes in client-side code, although you will still be able to display it in an Image component.

If you get an exception like Failed to load media:, the endpoint you are trying to access may not allow requests from client-side code. Try fetching the bytes from a Server Module or Uplink script instead.

See Browser Restrictions for more info.

BlobMedia gets its media from a byte-string you specify. This example makes a new BlobMedia containing a string of text, and then uploads it to an app file on Google Drive.

file_contents = "Hello, world".encode()      # String as bytes

my_media = anvil.BlobMedia(content_type="text/plain", content=file_contents, name="hello.txt")

You can also use JavaScript File objects and convert these to Anvil Media objects using anvil.js.to_media(). The following example gets a File from an HTML element and converts it to a Media object:

js_file = self.dom_nodes["my-file-component"].files[0]
file = anvil.js.to_media(js_file)

Uploading and downloading files


Files can be loaded into the browser using the FileLoader component.

To upload them to your server-side code, simply pass the FileLoader’s file property as an argument to a server function.

The uploaded file will be a Media object. These can be stored directly in Anvil’s Data Tables in a Media column.


The function allows you to download any Media object as a file in the user’s web browser. This example creates and downloads a file called hello.txt.


text_file = anvil.BlobMedia('text/plain', b'Hello, world', name='hello.txt') is available in Form code only.

Since you can store Media objects in your Data Tables, you can download files stored in Data Tables. Retrieve them in the same way as you would a string, integer or other data type. When the Media object is retrieved, it will still be a Media object; its format does not change from when it was originally loaded into the browser.

You can also create a download button with a Link. Media objects have a url property containing a URL that the user can access to download the file. Set this as a Link component’s url to give the user a clickable link that downloads the file. Media objects must be ‘permanent’ in order to have a URL - this means they must be stored in a Data Table, Google Drive, or originally retrieved from a URL.

Temporary URLs

You can also create temporary client-side URLs for Media Objects, even if the media has no permanent URL, using

from anvil.js import window
b = BlobMedia('text/plain', b"foo")

with as url:

This URL should be revoked when you are finished with it. If you use TempUrl as a context manager (with TempUrl(media) as url:), this happens automatically; if you instantiate it manually you must call revoke() on the instance.

temp_url =
url = temp_url.url

# When you're done, revoke the URL

Files in Server Modules

You can read and write to/from the filesystem in Server Modules just as you would in any Python environment. Your filesystem is your own; other users do not have access to it.

Writing to the filesystem requires an Personal Plan or higher.

def write_a_file(my_string):
  with open('/tmp/my-file.txt', 'w+') as f:
def read_a_file():
  with open('/tmp/my-file.txt', 'r') as f:
    contents =

Anvil also has methods to write to files from Media objects.

If you’re using a Python library that wants you to pass it a filename, this can be really useful for writing some data into a file, then passing the file_name to the library you’re using.


media_object = anvil.BlobMedia('text/plain', b'Hello, world', name='new.txt')

with as file_name:
    # Now there is a file in the filesystem containing the contents of media_object.
    # The file_name variable is a string of its full path.

Read from files to Media objects using this function:

import, [content_type], [name])

Do you still have questions?

Our Community Forum is full of helpful information and Anvil experts.