如何在python Flask框架中发送zip文件? [英] How to send zip files in the python Flask framework?

查看:2061
本文介绍了如何在python Flask框架中发送zip文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个烧录服务器,它从数据库中抓取几个不同文件的二进制数据,并把它们放到一个python的zipfile对象中。我想发送生成的zip文件与我的代码使用flask的send_file方法。

我最初能够通过使用BytesIO(bin )作为send_file的第一个参数,但出于某种原因,我不能用我生成的zip文件做同样的事情。它给出了错误:

ZipFile没有缓冲区接口。

如何发送这个zip文件对象与Flask用户?



这是我的代码:

 <$ c $ b $ def def downloadFiles():$ b $如果request.method =='POST':
mongo = MongoDAO('localhost',27017)
identifier = request.form ['CapsuleName']
password = request.form ['CapsulePassword']
result = mongo.getCapsuleByIdentifier标识符,密码)
zf = zipfile.ZipFile('capsule.zip','w')
files = result ['files']
用于文件中的个人文件:
数据= zipfile.ZipInfo(individualFile ['fileName'])
data.date_time = time.localtime(time.time())[:6]
data.compress_type = zipfile.ZIP_DEFLATED
zf .writestr(data,individualFile ['fileData'])
return send_file(BytesIO(zf),attachment_filename ='capsule.zip',as_attachment = True)
return render_template('download.html')
需要传递字节数据 ,但是 ZipFile()对象不是bytes-data;你实际上在你的硬盘上创建了一个文件
$ b $你可以创建一个 ZipFile()在内存中使用 BytesIO() 作为基础

  memory_file = BytesIO()
with zipfile.ZipFile(memory_file,'w')as zf:
files = result ['files']
files for individualFile:
data = zipfile.ZipInfo(individualFile ['fileName'])
data.date_time = time.localtime(time.time())[:6]
data.compress_type = zipfile.ZIP_DEFLATED
zf.writestr(data,individualFile ['fileData'])
memory_file.seek(0)
return send_file(memory_file,attachment_filename ='capsule.zip',as_attachment = True)

使用语句的确保 ZipFile()对象在完成添加条目时正确关闭,导致它将所需的尾部写入内存中的文件对象。需要 memory_file.seek(0)来将文件对象的读写位置倒带回到开始位置。


I have a flask server that grabs binary data for several different files from a database and puts them into a python 'zipfile' object. I want to send the generated zip file with my code using flask's "send_file" method.

I was originally able to send non-zip files successfully by using the BytesIO(bin) as the first argument to send_file, but for some reason I can't do the same thing with my generated zip file. It gives the error:

'ZipFile' does not have the buffer interface.

How do I send this zip file object to the user with Flask?

This is my code:

@app.route("/getcaps",methods=['GET','POST'])
def downloadFiles():
    if request.method == 'POST':
        mongo = MongoDAO('localhost',27017)
        identifier = request.form['CapsuleName']
        password = request.form['CapsulePassword']
        result = mongo.getCapsuleByIdentifier(identifier,password)
        zf = zipfile.ZipFile('capsule.zip','w')
        files = result['files']
        for individualFile in files:
            data = zipfile.ZipInfo(individualFile['fileName'])
            data.date_time = time.localtime(time.time())[:6]
            data.compress_type = zipfile.ZIP_DEFLATED
            zf.writestr(data,individualFile['fileData'])
        return send_file(BytesIO(zf), attachment_filename='capsule.zip', as_attachment=True)
    return render_template('download.html')

解决方案

BytesIO() needs to be passed bytes data, but a ZipFile() object is not bytes-data; you actually created a file on your harddisk.

You can create a ZipFile() in memory by using BytesIO() as the base:

memory_file = BytesIO()
with zipfile.ZipFile(memory_file, 'w') as zf:
    files = result['files']
    for individualFile in files:
        data = zipfile.ZipInfo(individualFile['fileName'])
        data.date_time = time.localtime(time.time())[:6]
        data.compress_type = zipfile.ZIP_DEFLATED
        zf.writestr(data, individualFile['fileData'])
memory_file.seek(0)
return send_file(memory_file, attachment_filename='capsule.zip', as_attachment=True)

The with statement ensures that the ZipFile() object is properly closed when you are done adding entries, causing it to write the required trailer to the in-memory file object. The memory_file.seek(0) call is needed to 'rewind' the read-write position of the file object back to the start.

这篇关于如何在python Flask框架中发送zip文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆