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.

相关推荐
LeonDL1687 分钟前
基于YOLO11深度学习的电动车头盔检测系统【Python源码+Pyqt5界面+数据集+安装使用教程+训练代码】【附下载链接】
人工智能·python·深度学习·pyqt5·yolo数据集·电动车头盔检测系统·yolo11深度学习
含若飞35 分钟前
Vue 中 `watch` 与 `this.$watch` 使用指南
前端·javascript·vue.js
无名小兵1 小时前
前端渲染大体积 多页面pdf
前端
c0detrend1 小时前
读诗的时候我却使用了自己研发的Chrome元素截图插件
前端·chrome
希冀1232 小时前
【Vue】第五篇
前端·javascript·vue.js
wxin_VXbishe2 小时前
springboot在线课堂教学辅助系统-计算机毕业设计源码07741
java·c++·spring boot·python·spring·django·php
Moonbit2 小时前
你行你上!MoonBit LOGO 重构有奖征集令
前端·后端·设计
littleplayer2 小时前
Root-> A ->B -> C page, 当前C page, 如何返回B,又如何直接返回A page呢
前端
姝然_95272 小时前
Android Activity启动流程详细分析
前端
littleplayer2 小时前
SwiftUI 导航
前端