One - One Code All

Blog Content

Flask下载文件

Python   2013-06-15 11:34:12

前言

由于最近在做文件管理模块的功能,所以难免会遇到文件上传下载这块的功能。不过文件上传那块是调用的OSS api,所以接触的不多。


文件的下载:


1. 接口返回真实的文件


这种情况比较简单, flask里带有此类api, 可以用send_from_directory和send_file.


核心代码如下:


from flask import send_file, send_from_directory

import os



@app.route("/download/", methods=['GET'])

def download_file(filename):

    # 需要知道2个参数, 第1个参数是本地目录的path, 第2个参数是文件名(带扩展名)

    directory = os.getcwd()  # 假设在当前目录

    return send_from_directory(directory, filename, as_attachment=True)


后边那个as_attachment参数需要赋值为True,不过此种办法有个问题,就是当filename里边出现中文的时候,会报错误。


解决办法:

使用flask自带的make_response


代码修改如下


from flask import send_file, send_from_directory

import os

from flask import make_response



@app.route("/download/", methods=['GET'])

def download_file(filename):

    # 需要知道2个参数, 第1个参数是本地目录的path, 第2个参数是文件名(带扩展名)

    directory = os.getcwd()  # 假设在当前目录

    response = make_response(send_from_directory(directory, filename, as_attachment=True))

    response.headers["Content-Disposition"] = "attachment; filename={}".format(file_name.encode().decode('latin-1'))

    return response


使用make_response函数建立一个response对象,然后将filename编码转为latin-1,可以看到server.py里边会严格按照latin-1编码来解析filename,所以我这里的做法是先将utf8编码的中文文件名默认转为latin-1编码。


2. 接口返回文件数据流


这种情况比较适合我现在的需求,因为我这边是用requests库,先请求一个oss链接,获取到文件的数据,然后我发现目前flask没有这样的api实现,这里还是使用make_response方法实现。


代码如下:


import mimetypes



@app.route('/fileManager/download///', methods=['GET'])

def download_file(projId, id, filename):

    try:

        url = "your url"

        r = requests.get(url, timeout=500)

        if r.status_code != 200:

            raise Exception("Cannot connect with oss server or file is not existed")

        response = make_response(r.content)

        mime_type = mimetypes.guess_type(filename)[0]

        response.headers['Content-Type'] = mime_type

        response.headers['Content-Disposition'] = 'attachment; filename={}'.format(filename.encode().decode('latin-1'))

        return response

    except Exception as err:

        print('download_file error: {}'.format(str(err)))

        logging.exception(err)

        return Utils.beop_response_error(msg='Download oss files failed!')


解释一下:

make_response很强大,下载一个文件,需要在response的headers里边添加一些信息,比如文件的类型,文件的名字,是否以附件形式添加,这3个是比较关键的信息。

mime_type是文件的类型,我观察send_file的源代码发现里边用到了mimetypes.guess_type()这个方法,也就是猜测文件的类型,然后这里我就直接搬过来用了哈哈,r.content其实就是文件的数据流,之前我是通过


with open(filename, 'wb') as file:

    file.write(r.content)


这样实现下载文件到本地的,所以其实r.content是一个文件数据流,也不清楚我的名词用的是否恰当哈哈。


之所以不用第一种方式,是因为我本地生成文件了之后,需要删除他,但是删除的时候总是会提示该文件已经被另一个程序使用,所以猜测是send_file这个api还在使用该文件,为了达到更好的效果,找到了第二种解决办法。


其实还有一种解决办法:


3. 发送静态文件


其实原来和第一种差不多,调用的api不一样,api是


from flask import app

import os



@app.route("/download/", methods=['GET'])

def download_file(filepath):

    # 此处的filepath是文件的路径,但是文件必须存储在static文件夹下, 比如images\test.jpg

    return app.send_static_file(filepath)  



上一篇:sklearn-SVC实现与类参数
下一篇:SQLAlchemy初步使用session.close()

The minute you think of giving up, think of the reason why you held on so long.