Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
829 views
in Technique[技术] by (71.8m points)

python - Streamlit, Flask, Fastapi: How to upload zipped files to my web app and make them downloadable?

I am working on an app using streamlit to upload a zipped file and enable users to download that zipped file. For a reference example, see how online services take in a word file, convert it into pdf and make that pdf downloadable automatically.

I have been able to complete the first 2 steps. Requesting assistance with uploading the file and making it downloadable. Thank you.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This is how it is done.

import streamlit as st
# zipping the different parts of the song
def zipdir(dst, src):
    zf = zipfile.ZipFile(dst, "w", zipfile.ZIP_DEFLATED)
    abs_src = os.path.abspath(src)
    for dirname, subdirs, files in os.walk(src):
        for filename in files:
            absname = os.path.abspath(os.path.join(dirname, filename))
            arcname = absname[len(abs_src) + 1:]
            print('zipping %s as %s' % (os.path.join(dirname, filename),
                                        arcname))
            zf.write(absname, arcname)
    zf.close()

zip_path = 'Python.zip'
zipdir(zip_path, path)

# send to webapp to make downloadable link
with open(zip_path, "rb") as f:
    bytes_read = f.read()
    b64 = base64.b64encode(bytes_read).decode()
    href = f'<a href="data:file/zip;base64,{b64}" download='{title}.zip'>
            Click to download
        </a>'
st.sidebar.markdown(href, unsafe_allow_html=True)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...