FLASK创建下载

html用a标签

html 复制代码
<!-- Button to download the image -->
<a href="{{ url_for('download_file', filename='image.png') }}">
   <button>Download Image</button>
</a>

后端:url_for双大括号即是用来插入变量到模板中的语法。也就是绑定了函数download_file()

python 复制代码
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.

相关推荐
吴维炜7 分钟前
「Python算法」计费引擎系统SKILL.md
python·算法·agent·skill.md·vb coding
徐同保21 分钟前
React useRef 完全指南:在异步回调中访问最新的 props/state引言
前端·javascript·react.js
FansyMeng1 小时前
VSCode配置anaconda
vscode·python
刘一说1 小时前
Vue 导航守卫未生效问题解析:为什么路由守卫不执行或逻辑失效?
前端·javascript·vue.js
电饭叔1 小时前
Tkinter Button 括号内的核心参数详解
python·学习
ktoking1 小时前
Stock Agent AI 模型的选股器实现 [五]
人工智能·python
地球资源数据云1 小时前
SCI制图——云雨图
python·信息可视化·数据分析
独自破碎E1 小时前
Spring Boot + LangChain4j 报错:Bean 类型不匹配的解决办法
spring boot·python·pycharm
一周七喜h1 小时前
在Vue3和TypeScripts中使用pinia
前端·javascript·vue.js
小W与影刀RPA2 小时前
【影刀 RPA】 :文档敏感词批量替换,省时省力又高效
人工智能·python·低代码·自动化·rpa·影刀rpa