python 详解如何写flask文件下载接口
作者:剑客阿良_ALiang 时间:2023-05-04 21:07:06
简述
写一个简单的flask文件下载接口。
依赖
flask、gevent
代码
不废话上代码。
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Oct 23 19:53:18 2021
@author: huyi
"""
from flask import Flask, request, make_response, send_from_directory
from gevent.pywsgi import WSGIServer
from gevent import monkey
# 将python标准的io方法,都替换成gevent中的同名方法,遇到io阻塞gevent自动进行协程切换
monkey.patch_all()
app = Flask(__name__)
@app.route("/download", methods=['GET'])
def download_file():
get_data = request.args.to_dict()
file_path = get_data.get('fileName')
response = make_response(
send_from_directory('/Users/huyi/Movies/Videos',file_path,as_attachment=True))
response.headers["Content-Disposition"] = "attachment; filename={}".format(
file_path.encode().decode('latin-1'))
return response
if __name__ == '__main__':
WSGIServer(('0.0.0.0', 8080), app).serve_forever()
准备数据:
浏览器输入:http://localhost:8080/download?fileName=test.mp4
下载完成。
来源:https://huyi-aliang.blog.csdn.net/article/details/120921334
标签:python,flask,文件,下载,接口
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Pyhton中单行和多行注释的使用方法及规范
2021-11-21 12:13:00
![](https://img.aspxhome.com/file/2023/0/135310_0s.jpg)
如何通过神经网络实现线性回归的拟合
2023-04-24 06:15:07
![](https://img.aspxhome.com/file/2023/7/64267_0s.jpg)
python 统计一个列表当中的每一个元素出现了多少次的方法
2021-09-21 13:48:32
三种Golang数组拷贝方式及性能分析详解
2023-07-13 07:54:27
支持png透明图片的php生成缩略图类分享
2023-11-18 07:26:13
![](https://img.aspxhome.com/file/2023/5/104175_0s.jpg)
Python中使用Flask、MongoDB搭建简易图片服务器
2021-12-09 19:57:16
php生成图片验证码的实例讲解
2023-09-11 21:36:29
![](https://img.aspxhome.com/file/2023/4/55574_0s.png)
解决在Python编辑器pycharm中程序run正常debug错误的问题
2023-09-09 09:58:39
![](https://img.aspxhome.com/file/2023/6/128206_0s.jpg)
golang 输出重定向:fmt Log,子进程Log,第三方库logrus的详解
2024-04-27 15:40:14
Design IT. (8),一匹“更快的马”
2009-02-11 12:19:00
详解vue 模版组件的三种用法
2024-05-10 14:18:22
python字典的setdefault的巧妙用法
2021-05-18 02:58:48
在mac下查找python包存放路径site-packages的实现方法
2023-06-12 21:12:36
各种鼠标经过图片边框加粗效果整理
2007-09-29 21:43:00
30步检查SQL Server安全列表
2008-12-18 14:28:00
一些关于Go程序错误处理的相关建议
2023-07-21 22:46:43
Pycharm直接使用远程服务器代码并调试的解决方法
2021-05-13 10:21:03
JavaScript选取(picking)和反选(rejecting)对象的属性方法
2023-08-24 22:28:47
uniapp H5 https跨域请求实现
2024-04-10 16:20:27
![](https://img.aspxhome.com/file/2023/0/136700_0s.png)
段正淳的css笔记(7)-表单在各浏览器的表现统一
2008-01-14 02:47:00