我在用弗斯克和福尔曼。我在内存中构建的数据,我希望用户能够下载文本文件中的数据。我不想将数据写出到本地磁盘上的文件中,然后将其提供给下载。
我是python的新手。我想我应该在内存中创建一些文件对象,然后设置响应头,也许吧?
发布于 2012-08-29 11:27:46
在Flask的文档中的"pattern“部分--具体地说,就是in the section on streaming --在没有保存到磁盘的情况下将文件流式传输到客户端。基本上,您要做的就是返回一个包装了迭代器的完整的Response
对象:
from flask import Response
# construct your app
@app.route("/get-file")
def get_file():
results = generate_file_data()
generator = (cell for row in results
for cell in row)
return Response(generator,
mimetype="text/plain",
headers={"Content-Disposition":
"attachment;filename=test.txt"})
https://stackoverflow.com/questions/12166970
复制相似问题