FLASK创建下载
html用a标签
<!-- Button to download the image -->
<a href="{{ url_for('download_file', filename='image.png') }}">
<button>Download Image</button>
</a>
后端:url_for双大括号即是用来插入变量到模板中的语法。也就是绑定了函数download_file()
from flask import Flask, render_template, send_from_directory
@app.route('/download/<filename>')
def download_file(filename):
# Send the image file to the client
return send_from_directory(app.config['UPLOAD_FOLDER'], filename,as_attachment=True)
Flask Route /download/<filename> : When the button is clicked, it triggers the /download/<filename> route, which uses send_from_directory to serve the image file stored in the static/images directory.