[英]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.