因为最近在作文件管理模块的功能,因此不免会遇到文件上传下载这块的功能。不过文件上传那块是调用的OSS api,因此接触的很少。python
这种状况比较简单, flask里带有此类api, 能够用send_from_directory和send_file.flask
核心代码以下:api
from flask import send_file, send_from_directory import os @app.route("/download/<filename>", 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里边出现中文的时候,会报以下错误:app
解决办法:
使用flask自带的make_response
代码修改以下函数
from flask import send_file, send_from_directory import os from flask import make_response @app.route("/download/<filename>", 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编码。编码
这种状况比较适合我如今的需求,由于我这边是用requests库,先请求一个oss连接,获取到文件的数据,而后我发现目前flask没有这样的api实现,这里仍是使用make_response方法实现。url
代码以下:code
import mimetypes @app.route('/fileManager/download/<projId>/<id>/<filename>', 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其实就是文件的数据流,以前我是经过orm
with open(filename, 'wb') as file: file.write(r.content)
这样实现下载文件到本地的,因此其实r.content是一个文件数据流,也不清楚个人名词用的是否恰当哈哈。server
之因此不用第一种方式,是由于我本地生成文件了以后,须要删除他,可是删除的时候老是会提示该文件已经被另外一个程序使用,因此猜想是send_file这个api还在使用该文件,为了达到更好的效果,找到了第二种解决办法。
其实还有一种解决办法:
其实原来和第一种差很少,调用的api不同,api是
from flask import app import os @app.route("/download/<filepath>", methods=['GET']) def download_file(filepath): # 此处的filepath是文件的路径,可是文件必须存储在static文件夹下, 好比images\test.jpg return app.send_static_file(filepath)