在Flask想要实现文件下载功能,只需要导入

send_from_directory(directory, filename, **options)

然后在视图函数中返回该函数即可

示例代码如下

from flask import Flask, send_from_directory

app = Flask(__name__)

@app.route("/download")
def index():
return send_from_directory(r"D:\desk\id",filename="123.jpg",as_attachment=True) if __name__ == '__main__':
print(app.url_map)
app.run(host="10.205.1.15", port=5000)

访问 http://10.205.1.15/download 即可下载123.jpg文件

05-20 05:20
查看更多