报错内容: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. IPC 之 Messenger 的使用

    一.概述 Messenger 是一种轻量级的 IPC 方案,它的底层实现是 AIDL ,对 AIDL 进行了封装,方便了对它的使用.Messenger 一次只处理一个请求,所以在服务端不用考虑线程同步 ...

  2. python模块(2)

    1.OS模块 print(os.getcwd()) #获取当前工作目录 os.chdir('E:\python练习\python35学习') #更改目录 print(os.getcwd()) E:\p ...

  3. 《剑指offer》第四十五题(把数组排成最小的数)

    // 面试题45:把数组排成最小的数 // 题目:输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼 // 接出的所有数字中最小的一个.例如输入数组{3, 32, 321},则打印出这3 ...

  4. QSplitter实现滑动窗口和悬浮按钮

    1         QSplitter实现滑动窗口和悬浮按钮 软件应用中需要设计右侧滑动窗口,通过一个按钮来实现窗口的隐藏和显示,应用场景比如显示主界面的详细信息. (1)   在qt design中 ...

  5. Integer与int区别

    Integer与int的区别:估计大多数人只会说道两点,一开始我也不太清楚,Ingeter是int的包装类,int的初值为0,Ingeter的初值为null.但是如果面试官再问一下Integer i ...

  6. java---->lombok.jar的使用

    lombok.jar是在看一个项目的代码时遇见的一个“新包”,记录下用法: 1.新建maven工程myfirst,新建一个javabean User,写一个LombokTest类,在pom.xml引入 ...

  7. 自定义WPF控件(MyTextBox、MyDatePicker、MyDataGrid)

    方案一:样式与代码结合 按照UserControl的前后台结合的方式编写MyControl,前台写样式,后台写功能.规则等(创建一个UserControl,然后修改一下即可) 方案二:样式与代码分离 ...

  8. pyhon基础之约束和异常处理:

    约束和异常处理:内容梗概: 1. 类的约束 2. 异常处理 3. 自定义异常 4. 日志 1. 类的约束定义:约束是对类的约束常用方法:1. 提取父类.然后在父类中定义好方法.在这个方法中什么都不用干 ...

  9. 牛客寒假算法基础集训营6 J-迷宫

    题目请点这里 分析:这是一道BFS的模板题,构造一个队列,将每个满足条件的(不超过边界,不超过左右移动次数的限制)位置推入队列,如果不是障碍物且没到达过,就将可到达位置的个数加1 此外,注意这里的输入 ...

  10. Selenium-WebDriverApi接口详解

    浏览器操作 # 刷新 driver.refresh() # 前进 driver.forward() # 后退 driver.back() 获取标签元素 # 通过ID定位目标元素 driver.find ...