How to Save Uploaded Files to Directory in Streamlit Apps

Sometime ago we learnt about how to upload files and process the uploaded files in Streamlit. We dived deeper into the Streamlit UploadedFile Class and how it reads it as a file-like Byte IO type.

In this tutorial we will learn about how to save our uploaded file into a directory. This is useful in case you want to save all the uploaded file and use them later.

Let us see how to go about that.

By the end of this post, you will learn about;

  • How to upload files in streamlit using st.file_uploader()
  • How to save the uploaded file to a directory/folder

To make it quite simple – any uploaded file is treated as a file-like Byte-IO type. Hence you can write that file using python’s standard library IO utils.

This approach makes it quite easier to both process and save our file by just using the write() function in python and the .getbuffer() of the uploaded file.

To save our uploaded file we can write that file in the “wb” format to a location of our choice. We can use os.path to point us to the directory we want to save it . Don’t forget to create the directory you want to save your files to eg tempDir

In order to save the uploaded file with the same name we can use the .name attribute of our uploadedFile class . Putting all of them together

 with open(os.path.join("tempDir",,"wb") as f:

We can then place this code under our uploaded file section as below

image_file = st.file_uploader("Upload An Image",type=['png','jpeg','jpg'])
if image_file is not None:
    file_details = {"FileName","FileType":image_file.type}
    img = load_image(image_file)
    with open(os.path.join("tempDir",,"wb") as f: 
    st.success("Saved File")

We can also convert it into a function so that it is reusable everywhere and anytime.

def save_uploadedfile(uploadedfile):
     with open(os.path.join("tempDir",,"wb") as f:
     return st.success("Saved File:{} to tempDir".format(

Now we can easily use it like this

datafile = st.file_uploader("Upload CSV",type=['csv'])
if datafile is not None:
   file_details = {"FileName","FileType":datafile.type}
   df  = pd.read_csv(datafile)

To conclude we have seen how to save our uploaded file into a directory in our Streamlit app. Streamlit is amazing. You can also check out the video tutorial and other courses here.

Thanks For Your Time

Jesus Saves

By.Jesse E.Agbe(JCharis)

8 thoughts on “How to Save Uploaded Files to Directory in Streamlit Apps”

  1. Hello Jesse ,
    so this is the error message i am getting when i try to execute the second block of code on your blog :

    NameError: name ‘load_image’ is not defined

    PS: Was load image defined anywhere ?

    1. Hello Benniah,
      The load_image function is a separate function (From the Working with Uploads Tutorial)
      But you can also add it
      # Image
      from PIL import Image

      def load_image(image_file):
      img =
      return img

      Hope this helps

  2. Thanks a lot Jesse !

    I found a workaround for multiple file uploads by using a Loop:

    import streamlit as st
    import os

    def save_uploadedfile(uploadedfile):
    with open(os.path.join(“Data”,, “wb”) as f:
    return st.success(“Saved File:{} to Data”.format(

    st.title(” PDF File upload”)
    st.text(” A simple way to upload files directly into a directory”)
    uploadedfiles = st.file_uploader(“Upload PDF”, type=[‘pdf’], accept_multiple_files=True)
    for file in uploadedfiles:
    if uploadedfiles is not None:

  3. Bro! After a attempting a model training of an ml algo. Suddenly the streamlit app stops running. But, the background process of training is going on terminal. So, if we refresh again the training starts from beginning.
    Is there any way to store the trained data into a csv file after the training the data gets completed.

    1. jesse_jcharis

      Hello Tirumalesh, yes you can do that – you can also use featurestore for this. Moreover it is usually recommended to deploy the pre-trained model (batch ml) for the app to increase performance instead of training on the go (online ml).

Leave a Comment

Your email address will not be published. Required fields are marked *