python flask下载 python 详解怎样写flask文件下载接口

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

python flask下载 python 详解怎样写flask文件下载接口

剑客阿良_ALiang   2021-10-28 我要评论
想了解python 详解怎样写flask文件下载接口的相关内容吗,剑客阿良_ALiang在本文为您仔细讲解python flask下载的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:python,flask下载,python,flask文件下载接口,下面大家一起来学习吧。

简述

写一个简单的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

下载完成。

总结

没啥好总结的。

如果本文对你有帮助,请点个赞支持一下吧。

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们