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.

相关推荐
虚空之月&&轮舞者1 小时前
Python与矢量网络分析仪3671E:自动化测试(Vscode)
网络·vscode·python·射频工程
李昊哲小课1 小时前
tensorflow-cpu
大数据·人工智能·python·深度学习·数据分析·tensorflow
2501_915373884 小时前
Vue 3零基础入门:从环境搭建到第一个组件
前端·javascript·vue.js
小彭律师6 小时前
数字化工厂中央控制室驾驶舱系统架构文档
python
沙振宇7 小时前
【Web】使用Vue3开发鸿蒙的HelloWorld!
前端·华为·harmonyos
old_power7 小时前
【Python】PDF文件处理(PyPDF2、borb、fitz)
python·pdf
运维@小兵8 小时前
vue开发用户注册功能
前端·javascript·vue.js
测试开发Kevin8 小时前
从投入产出、效率、上手难易度等角度综合对比 pytest 和 unittest 框架
python·pytest
强化学习与机器人控制仿真8 小时前
Newton GPU 机器人仿真器入门教程(零)— NVIDIA、DeepMind、Disney 联合推出
开发语言·人工智能·python·stm32·深度学习·机器人·自动驾驶
蓝婷儿8 小时前
前端面试每日三题 - Day 30
前端·面试·职场和发展