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 小时前
PySide6从0开始学习的笔记(四)QMainWindow
笔记·python·学习·pyqt
深蓝海拓1 小时前
PySide6 的 QSettings简单应用学习笔记
python·学习·pyqt
码界奇点8 小时前
Python从0到100一站式学习路线图与实战指南
开发语言·python·学习·青少年编程·贴图
Laravel技术社区9 小时前
pytesseract 中英文 识别图片文字
python
天天扭码9 小时前
如何实现流式输出?一篇文章手把手教你!
前端·aigc·ai编程
前端 贾公子9 小时前
vue移动端适配方案 === postcss-px-to-viewport
前端·javascript·html
生骨大头菜10 小时前
使用python实现相似图片搜索功能,并接入springcloud
开发语言·python·spring cloud·微服务
绝不收费—免费看不了了联系我10 小时前
Fastapi的单进程响应问题 和 解决方法
开发语言·后端·python·fastapi
xqqxqxxq10 小时前
背单词软件技术笔记(V2.0扩展版)
java·笔记·python
GISer_Jing10 小时前
AI营销增长:4大核心能力+前端落地指南
前端·javascript·人工智能