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'}
)
输出
raise BadZipFile("File is not a zip file")
zipfile.BadZipFile: File is not a zip file
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'})