St.file_uploader() removing file after any triggered event

While using the st.file_uploader() feature in the latest release of streamlit==0.69.0, I noticed that after you successfully upload a file, the file is immediately removed after any action such as pressing a button or selecting an option from the st.mulitselect() and I am faced with the following error message:

‘cannot read an empty file’

It seems as though everything is being reset including the uploaded file. This was not the case in streamlit==0.60.0.

1 Like

It’s likely that you need to reset your buffer with uploaded_file.seek(0) .

Previously we were creating a new buffer for you each time we reran. To optimize, we are returning the same buffer on rerun. Unfortunately, this means that if you’ve already read the buffer, you’ll need to reset after. If you use .getValue(), there’s no need to seek. If you are working with certain libraries, they are likely performing a read().

multiple_files = st.file_uploader('label', accept_multiple_files=True)
for file in multiple_files:
	file_body = file.read()
	file.seek(0)
2 Likes

Thanks! It works just fine now.

Sorry, but on a similar note I have a problem where my dataframes are being lost after I press a button. Is there anyway to preserve a dataframe?

Could you clarify what you mean by dataframes are being lost? Is the dataframe populated with data from a file uploader and the contents of the file uploader is disappearing? Sample code would be super helpful

That code only taking file input, in that how we can call the data?
Please help.

Hi @kushalvishwak,

Can you clarify what data you are trying to access? st.file_uploader returns the uploaded file as a file like object. If you have accept_multiple_files set to True, it will return a list of file like objects. If no file(s) have been uploaded, it will return an empty list or None.

Once you upload a file, you can handle the object as you would a BytesIO.

Hopefully this helps!