1、创建一个get_file.py的python文件,内容如下所示:
# -*- coding: utf-8 -*-
from flask import Flask, render_template, send_file, send_from_directory,json, jsonify,make_response
app = Flask(__name__) #实例化flask app
#file_name是客户端传来的需要下载的文件名
@app.route('/get_file/<file_name>', methods=['GET'])
def get_file(file_name):
directory = config.APP_PATH
try:
response = make_response(
send_from_directory(directory, file_name, as_attachment=True))
return response
except Exception as e:
return jsonify({"code": "异常", "message": "{}".format(e)})
if __name__ == '__main__':
app.run(debug=False, host='0.0.0.0', port=80)
2、执行get_file.py文件,当服务启动后,在浏览器地址输入http://localhost:80/get_file/filename访问,就可以下载文件了。