Flask/Werkzeug how to attach HTTP content-length header to file download

寵の児 提交于 2019-11-28 21:24:43

I believe you'd do something like this (untested):

from flask import Response
response = Response()
response.headers.add('content-length', str(os.path.getsize(FILE_LOCATION)))

See: Werkzug's Headers object and Flask's Response object.

Cyril N.

I needed this also, but for every requests, so here's what I did (based on the doc) :

@app.after_request
def after_request(response):
    response.headers.add('Access-Control-Allow-Origin', '*')
    return response

Since version 0.6 the canonical way to add headers to a response object is via the make_response method (see Flask docs).

def index():
    response = make_response(render_template('index.html', foo=42))
    response.headers['X-Parachutes'] = 'parachutes are cool'
    return response
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!