1

I have created a form with multiple text input values and one pdf upload. But I also want to save the pdf file uploaded to one my directory folder , How can I do that?

st.title("Demo")
st.image(res, width = 800)

st.markdown("**Please fill the below form :**")
with st.form(key="Form :", clear_on_submit = True):
    Name = st.text_input("Name : ")
    Email = st.text_input("Email ID : ")
    File = st.file_uploader(label = "Upload Pic", type=["pdf","docx"])
    Submit = st.form_submit_button(label='Submit')
    

st.subheader("Details : ")
st.metric(label = "Name :", value = Name)
st.metric(label = "Email ID :", value = Email)

if Submit :
    st.markdown("**Pic Sucessfully Uploaded.**")
    
John
  • 65
  • 1
  • 7

2 Answers2

1

The answer to this depends on where its being deployed.

Local machine (or server you have control of): Save it to anywhere you want on the local filesystem using the standard Python write mechanism:

with open(filename, 'wb') as f: 
    f.write(filebytes)

https://stackoverflow.com/a/70157307/2394542

Streamlit Cloud: because of the container deployment, you cannot guarantee that a specific container will be running, that multiple containers don't exist, etc. In this case, you should write to Google Drive, Amazon S3 or any other permanent location outside of Streamlit Cloud.

Randy Zwitch
  • 1,994
  • 1
  • 11
  • 19
0

Sample code to save locally from a given folder the uploaded file.

import streamlit as st
from pathlib import Path


st.title("Demo")
# st.image(res, width = 800)

st.markdown("**Please fill the below form :**")
with st.form(key="Form :", clear_on_submit = True):
    Name = st.text_input("Name : ")
    Email = st.text_input("Email ID : ")
    File = st.file_uploader(label = "Upload file", type=["pdf","docx"])
    Submit = st.form_submit_button(label='Submit')
    

st.subheader("Details : ")
st.metric(label = "Name :", value = Name)
st.metric(label = "Email ID :", value = Email)

if Submit :
    st.markdown("**The file is sucessfully Uploaded.**")

    # Save uploaded file to 'F:/tmp' folder.
    save_folder = 'F:/tmp'
    save_path = Path(save_folder, File.name)
    with open(save_path, mode='wb') as w:
        w.write(File.getvalue())

    if save_path.exists():
        st.success(f'File {File.name} is successfully saved!')

Ref: file_uploader

ferdy
  • 4,396
  • 2
  • 4
  • 16