flask 实现文件下载功能

0 评论
/ /
1041 阅读
/
697 字
30 2018-07
from flask import send_file, send_from_directory
import os

@web_user.route("/download/", methods=['GET'])
def download_file():
    filename = '123456.xls'
    root_path = get_root_path()
    file_path = root_path + '/zhanlu/static/uploads/'
    directory = os.getcwd()
    return send_from_directory(file_path, filename, as_attachment=True)

上述代码中的get_root_path方法放置在项目的根目录下,然后调用,代码内容如下

#!/usr/bin/env python
#coding=utf-8
import os


def get_root_path():
    project_dir = os.path.dirname(os.path.abspath(__file__))
    return project_dir