[英]how to zip files from url and then return zip file as response in flask
def generate_files_link():
url = 'https://www.facebook.com/favicon.ico'
r = requests.get(url, allow_redirects=True)
z=zipfile.ZipFile(io.BytesIO(r.content))
return Response( z,mimetype='application/zip',headers={'Content-Disposition':
'attachment;filename=files.zip'})
output
raise BadZipFile("File is not a zip file")
zipfile.BadZipFile: File is not a zip file
您需要從此處提到的 zipfile 模塊調用ZipFile.writestr()
方法,以將字符串轉換為 zip 文件。
def generate_files_link():
url = 'https://www.facebook.com/favicon.ico'
r = requests.get(url, allow_redirects=True)
print(r.content)
z = zipfile.ZipFile.writestr(
zinfo_or_arcname="files.zip", data=io.BytesIO(r.content))
return Response(z, mimetype='application/zip', headers={'Content-Disposition': 'attachment;filename=files.zip'})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.