Streamlit、Flask、Fastapi:如何将压缩文件上传到我的网络应用程序并使其可下载?

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

我正在开发一个使用 streamlit 上传压缩文件并允许用户下载该压缩文件的应用程序。有关参考示例,请参阅在线服务如何接收 word 文件,将其转换为 pdf 并使该 pdf 可下载自动

我已经完成了前 2 个步骤。请求帮助上传文件并使其可下载。谢谢。

这是怎么做的。

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)