继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

flask文件下载接口的简单写法

幕布斯7119047
关注TA
已关注
手记 432
粉丝 28
获赞 99

    今天给大家分享一个flask文件下载接口的简单写法,话不多说直接上代码:

#!/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()

    以上便是flask文件下载接口的简单写法全部内容,同学们学会了吗,更多问题可在评论区留言~


打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP