报错内容:TypeError: make_conditional() got an unexpected keyword argument 'accept_ranges'

  报错行自己代码如下:

directory = os.path.join(current_app.root_path, "data")

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

  这个问题,刚开始用apache部署后出现过,后来用nginx没有出现这个问题,今天追了下代码,源码如下:

# 安装包flask/helper.py

# 667行send_from_directory方法
def send_from_directory(directory, filename, **options):
"""Send a file from a given directory with :func:`send_file`. This
is a secure way to quickly expose static files from an upload folder
or something similar. Example usage:: @app.route('/uploads/<path:filename>')
def download_file(filename):
return send_from_directory(app.config['UPLOAD_FOLDER'],
filename, as_attachment=True) .. admonition:: Sending files and Performance It is strongly recommended to activate either ``X-Sendfile`` support in
your webserver or (if no authentication happens) to tell the webserver
to serve files for the given path on its own without calling into the
web application for improved performance. .. versionadded:: 0.5 :param directory: the directory where all the files are stored.
:param filename: the filename relative to that directory to
download.
:param options: optional keyword arguments that are directly
forwarded to :func:`send_file`.
"""
filename = safe_join(directory, filename)
if not os.path.isabs(filename):
filename = os.path.join(current_app.root_path, filename)
try:
if not os.path.isfile(filename):
raise NotFound()
except (TypeError, ValueError):
raise BadRequest()
options.setdefault('conditional', True)
return send_file(filename, **options) # 返回send_file方法 同文件454行
def send_file(filename_or_fp, mimetype=None, as_attachment=False,
attachment_filename=None, add_etags=True,
cache_timeout=None, conditional=False, last_modified=None):
# 省略多行 下行为625 找到报错的make_conditional方法了
if conditional:
try:
rv = rv.make_conditional(request, accept_ranges=True,
complete_length=fsize)
except RequestedRangeNotSatisfiable:
if file is not None:
file.close()
raise
# make sure we don't send x-sendfile for servers that
# ignore the 304 status code for x-sendfile.
if rv.status_code == 304:
rv.headers.pop('x-sendfile', None)
return rv

  现在反过来看自己的代码,使用的是send_from_directory,为了从指定目录读取,这个方法最后也是会去调用send_file,所以直接使用send_file下载文件到客户端,代码修改后如下:

from flask import make_response,send_file

directory = os.path.join(current_app.root_path, "data")

# 修复TypeError: make_conditional() got an unexpected keyword argument 'accept_ranges'
response = make_response(send_file(str(directory) + '/' + filename, as_attachment=True))

  问题解决。

flask下载zip文件报错TypeError的更多相关文章

  1. 导入json文件报错,TypeError expected string or buffer

    导入json文件报错,TypeError expected string or buffer 原因:用字符串赋值后,python会把双引号转换为单引号 import json data = [{&qu ...

  2. maven 的 pom.xml 文件报错:ArtifactTransferException: Failure to transfer

    因为maven下载依赖jar包时,特别慢,所以取消了下载过程,再次打开eclipse时,maven的pom.xml文件报错如下: ArtifactTransferException: Failure ...

  3. 彻底解决asp.net mvc5.2.2:vs2013 cshtml视图文件报错(当前上下文中不存在名称“model”,ViewBag,Url)

    最近遇到一个奇葩的问题,在vs2013下cshtml视图文件报错,出现当前上下文中不存在名称“model”,ViewBag,Url等等),在视图中也没有智能提示了,用@model声明视图的model类 ...

  4. 导入项目后,js文件报错解决方法

    导入项目后,发现 js文件报错,但是js文件是从官网下载的.解决办法: 选中报错的js文件, 右键选择 MyEclipse-->Exclude From Validation : 然后继续右键执 ...

  5. namenode磁盘满引发recover edits文件报错

    前段时间公司hadoop集群宕机,发现是namenode磁盘满了, 清理出部分空间后,重启集群时,重启失败. 又发现集群Secondary namenode 服务也恰恰坏掉,导致所有的操作log持续写 ...

  6. Linux - xshell上传文件报错乱码

    xshell上传文件报错乱码,解决方法 rz -be 回车 下载sz  filename

  7. 下载uhd_images_downloader出现报错

    下载uhd_images_downloader出现报错 [INFO] Images destination: /usr/local/share/uhd/images [INFO] No invento ...

  8. Maven无法导入插件,pom文件报错

    最近在使用IDEA导入开源项目bootshiro,更新依赖的时候,发现有些插件无法导入,以致于pom文件一直报找不到该插件的错误 一开始就网上各种百度,无论怎么更换阿里云的镜像都导不进,最后想着试试自 ...

  9. 第一次打开pycharm运行python文件报错”No Python interpreter selected“问题的解决办法

    前面没有细讲,这里细述一下安装pycharm后,第一次打开pycharm运行python文件报错"No Python interpreter selected"问题的解决办法. 出 ...

随机推荐

  1. sort-插入排序

    void sort_insertion(vector<int> &v) { for(int i=1;i<v.size();i++) { for(int j=i;j>0; ...

  2. [C#]获取指定文件夹下的所有文件名(递归)

    典型的递归方法: //定义一个list集合 List<String> list = new List<String>(); public void director(strin ...

  3. 图片方向 image orientation Exif

    更新 : 2019-01-02 refer https://stackoverflow.com/questions/3129099/how-to-flip-images-horizontally-wi ...

  4. 第 8 章 容器网络 - 049 - 准备 Overlay 网络实验环境

    overlay环境准备 在docker-machine (10.12.31.21)的基础上 docker主机 host1 (10.12.31.22) host2 (10.12.31.23) 在dock ...

  5. window如何安装redis服务、卸载redis服务和启动redis服务

    window如何安装redis服务.卸载redis服务和启动redis服务 一.总结 一句话总结:github上下载,解压,命令行运行(redis-server.exe redis.windows.c ...

  6. joomla 的语言翻译

    最近装了留言板组件 phocaguestbook,发觉没有中文翻译. 于是在 components\com_phocaguestbook\language\en-GB 找到了英文的文件. 依葫芦画瓢, ...

  7. Oracle DB , 计算各个用户/schema 的磁盘占用空间

    http://www.dba-oracle.com/t_find_size_schema.htm Question:  How do I find the size of a schema in my ...

  8. Getting started with Processing 第六章总结

    平移,旋转和缩放 前言 在这一章节中,平移.旋转和缩放都是以原点为基准的.并且是通过控制坐标系原点的位置来达到图元平移,旋转.缩放的视觉效果.作者在文中的原话是:另一种在屏幕上改变位置和移动物体的技术 ...

  9. 如何通过 Terminal 设置截图存储的位置

    由于截图的默认位置是存储在 desktop 上的,而 desktop 又是我们经常见的地方,有一个干净的界面对良好的电脑的体验非常重要,那么可以通过改变截图的默认的存储位置来改变.而改变这一路径需要用 ...

  10. English trip EM2-PE-1B Teacher:Patirck

    PE = 演讲课 课上内容(Lesson) How are you today?  你今天怎么样? What is your name?  你的名字叫什么? What do you come from ...