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



我正在开发一个应用程序,使用streamlit上传压缩文件,并使用户能够下载该压缩文件。作为一个参考示例,请参阅在线服务如何接收单词文件,将其转换为pdf,并使该pdf自动下载。

我已经能够完成前两个步骤。请求帮助上传文件并使其可下载。非常感谢。

就是这样做的。

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)

最新更新