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.

相关推荐
非凡ghost20 小时前
AOMEI Partition Assistant磁盘分区工具:磁盘管理的得力助手
linux·运维·前端·数据库·学习·生活·软件需求
m0_5782678620 小时前
从零开始的python学习(九)P142+P143+P144+P145+P146
笔记·python·学习
is081520 小时前
You Only Look Once
python
UrbanJazzerati21 小时前
前端入门:margin居中、border、box-radius、transform、box-shadow、mouse事件、preventDefault()
前端·面试
蝎子莱莱爱打怪21 小时前
🚀🚀🚀嗨,一起来开发 开源IM系统呀!
前端·后端·github
zqy022721 小时前
HTTP的Web服务测试在Python中的实现
python·网络协议·http
Enddme21 小时前
《前端笔试必备:JavaScript ACM输入输出模板》
前端·javascript·面试
前端鱼21 小时前
前端面试中值得关注的js题
前端·面试
豌豆花下猫21 小时前
Python 潮流周刊#119:Google 停止开发 Pytype!
后端·python·ai
千册21 小时前
pyside6 的pdf显示测试 -- 01
开发语言·python·pdf