简体   繁体   中英

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

You need to call the ZipFile.writestr() method from the zipfile module as mentioned here to convert a string to a zip file.

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'})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM