簡體   English   中英

如何從 url 中獲取 zip 文件,然后返回 zip 文件作為 Z319C3206A7F10C1457C3BZ9116D 中的響應

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

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM