How to upload folder on Anvil Form

Not an entire app. A code snippet should suffice.

To be fair, there is a little bit of a trick to it. The value you get out of a FileLoader or database column will be a Media Object, but Python’s zipfile library (and its ZipFile class) naturally doesn’t know anything about Media Objects. So a direct connection between the two isn’t possible. There will be an intermediate step, to convert the Media Object into something that ZipFile does understand: Python’s bytes type.

In the code below, report_blob is a Media Object.

    if report_blob.content_type != 'application/zip':
        return { 'err': 'Not a Zip File.' }
    zip_as_in_memory_file = io.BytesIO(report_blob.get_bytes())
    z = zipfile.ZipFile(zip_as_in_memory_file)

At that point, z is a ZipFile instance, which you already know about.

I hope this helps.

1 Like