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.

相关推荐
李伟_Li慢慢6 分钟前
13-mini版WebGLProgram
前端·three.js
李伟_Li慢慢8 分钟前
14-mini版WebGLPrograms
前端·three.js
李伟_Li慢慢12 分钟前
19-mini版WebGLRenderer
前端·three.js
李伟_Li慢慢31 分钟前
10-mini版bindingStates
前端·three.js
李伟_Li慢慢33 分钟前
08-BufferAttribute
前端·three.js
2501_9419820534 分钟前
企业微信二次开发:如何用 Python 搭建通用的 Webhook 实时消息接收
开发语言·python·企业微信
在世修行34 分钟前
第24篇:PyInstaller打包实战 — 从Python脚本到Windows EXE
人工智能·python·pyinstaller
李伟_Li慢慢34 分钟前
09-mini版WebGLAttributes
前端·three.js
李伟_Li慢慢35 分钟前
11-mini版WebGLGeometries
前端·three.js
李伟_Li慢慢36 分钟前
18-mini版WebGLRenderList
前端·three.js