1 后台程序直接生成文件内容
from flask import make_response
@app.route('/testdownload', methods=['GET'])
def testdownload():
content = "long text"
response = make_response(content)
response.headers["Content-Disposition"] = "attachment; filename=myfilename.txt"
return response
2 读取一个服务器上的文件,供用户下载
from flask import make_response , send_file
@app.route('/testdownload', methods=['GET'])
def testdownload():
response = make_response(send_file("views.py"))
response.headers["Content-Disposition"] = "attachment; filename=views.py;"
return response