为了账号安全,请及时绑定邮箱和手机立即绑定

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

标签:
Flask

    今天给大家分享一个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文件下载接口的简单写法全部内容,同学们学会了吗,更多问题可在评论区留言~


点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消