1.send_file() / make_response()
from flask import Flask
from flask import send_file,make_response
app = Flask(__name__)
@app.route('/download')
def downloadFile():
path = "test.txt"
# return send_file(path,as_attachment=True) # if remove the as_attachment=True will return the content of the txt and display on the page
response = make_response(send_file(path, as_attachment=True))
response.headers["Content-Disposition"] = "attachment; filename={}".format(filename.encode().decode('latin-1'))
response.headers["Content-length"] = fsize #组装返回的response
return response
if __name__ == '__main__':
app.run()
2. request 的content-type 是application/json , 代码读取json body 就用request.json.get("data"); content-type 是application/x-www-form-urlencoded, 代码读取json body 就用request.form.get('data')