Does switching file_uploader keys reset uploaded files?

Hi all,

I’m working with st.file_uploader and noticed that when I switch between different key values, the uploaded file disappears.

Minimal example:

import streamlit as st

toggle = st.checkbox("Use alternate uploader key")
key = "uploader_alt" if toggle else "uploader"

uploaded_file = st.file_uploader("Upload a file", key=key)

if uploaded_file:
    st.write(f"Uploaded file: {uploaded_file.name}")

If I upload a file, then toggle the checkbox (changing the key), the uploader resets and I lose the file. Switching back to the original key doesn’t restore it either.

Is this the expected behavior? If so, is there a workaround to persist the uploaded file when changing keys?

Thanks!

Yes.

I am not sure I understand. Of course you can store the uploaded file in tha session state, but that doesn’t make much sense to me. Why would you want to change the key and create a new widget if you want to keep the file?