我想通过werkzeug流式传输一个大文件 目前我的wsgi应用程序如下所示:
from werkzeug.wrappers import Request, Response
from werkzeug.wsgi import ClosingIterator, wrap_file
import os
class Streamer(object):
def __init__(self):
pass
def __call__(self, environ, start_response):
request = Request(environ)
filename = os.getcwd() + "/bigfile.xml"
try:
response = wrap_file(environ, open(filename) )
return response
except HTTPException, e:
response = e
return ClosingIterator(response(environ, start_response))
我不确定我应该对wrap_file函数返回的对象做什么。
答案 0 :(得分:18)
没有尝试过,但我认为以下方法可行。
g = file(path_to_bigfile) # or any generator
return Response(g, direct_passthrough=True)
答案 1 :(得分:0)
以防万一,另外: 1.保留文件名 2.发出没有页面重定向的下载
# file_name assumed to be known
# file_path assumed to be known
file_size = os.path.getsize(file_path)
fh = file(file_path, 'rb')
return Response(fh,
mimetype='application/octet-stream',
headers=[
('Content-Length', str(file_size)),
('Content-Disposition', "attachment; filename=\"%s\"" % file_name),
],
direct_passthrough=True)