flask下载zip文件报错TypeError
报错内容: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的更多相关文章
- 导入json文件报错,TypeError expected string or buffer
		
导入json文件报错,TypeError expected string or buffer 原因:用字符串赋值后,python会把双引号转换为单引号 import json data = [{&qu ...
 - maven 的 pom.xml 文件报错:ArtifactTransferException: Failure to transfer
		
因为maven下载依赖jar包时,特别慢,所以取消了下载过程,再次打开eclipse时,maven的pom.xml文件报错如下: ArtifactTransferException: Failure ...
 - 彻底解决asp.net mvc5.2.2:vs2013 cshtml视图文件报错(当前上下文中不存在名称“model”,ViewBag,Url)
		
最近遇到一个奇葩的问题,在vs2013下cshtml视图文件报错,出现当前上下文中不存在名称“model”,ViewBag,Url等等),在视图中也没有智能提示了,用@model声明视图的model类 ...
 - 导入项目后,js文件报错解决方法
		
导入项目后,发现 js文件报错,但是js文件是从官网下载的.解决办法: 选中报错的js文件, 右键选择 MyEclipse-->Exclude From Validation : 然后继续右键执 ...
 - namenode磁盘满引发recover edits文件报错
		
前段时间公司hadoop集群宕机,发现是namenode磁盘满了, 清理出部分空间后,重启集群时,重启失败. 又发现集群Secondary namenode 服务也恰恰坏掉,导致所有的操作log持续写 ...
 - Linux - xshell上传文件报错乱码
		
xshell上传文件报错乱码,解决方法 rz -be 回车 下载sz filename
 - 下载uhd_images_downloader出现报错
		
下载uhd_images_downloader出现报错 [INFO] Images destination: /usr/local/share/uhd/images [INFO] No invento ...
 - Maven无法导入插件,pom文件报错
		
最近在使用IDEA导入开源项目bootshiro,更新依赖的时候,发现有些插件无法导入,以致于pom文件一直报找不到该插件的错误 一开始就网上各种百度,无论怎么更换阿里云的镜像都导不进,最后想着试试自 ...
 - 第一次打开pycharm运行python文件报错”No Python interpreter selected“问题的解决办法
		
前面没有细讲,这里细述一下安装pycharm后,第一次打开pycharm运行python文件报错"No Python interpreter selected"问题的解决办法. 出 ...
 
随机推荐
- 2019热门JAVA面试问题
			
收到不少读者反馈,说自己在应聘一些中大型互联网公司的Java工程师岗位时遇到了不少困惑. 这些同学说自己也做了精心准备,网上搜集了不少Java面试题,然而实际去互联网公司面试才发现,人家问的,和你准备 ...
 - 学习笔记45—Linux压缩集
			
1.压缩功能 安装 sudo apt-get install rar 卸载 sudo apt-get remove rar 2.解压功能 安装 sudo apt-get install unrar 卸 ...
 - AtCoder Regular Contest 102 D - All Your Paths are Different Lengths
			
D - All Your Paths are Different Lengths 思路: 二进制构造 首先找到最大的t,使得2^t <= l 然后我们就能构造一种方法使得正好存在 0 到 2^t ...
 - cyberduck的SSH登录
			
1.通过配置SSH秘钥. 2.不点匿名(不要点匿名),如果非要填一个名字的话,你写root就行. 3.书签.
 - Spring Boot入门第三天:配置日志系统和Druid数据库连接池。
			
原文链接 一.日志管理 1.在application.properties文件中加入如下内容: logging.level.root=WARN logging.level.org.springfram ...
 - English trip V1 - 19.Where Am I? 我在哪里?Teacher:Patrick Key:Ask for and directions
			
In this lesson you will learn to ask for and give directions. 本节课你将学习到学会问路和指路. 课上内容(Lesson) 人类的几种感: ...
 - 20171114xlVba选定单行记录并打印
			
Public Sub PrintSelectRow() Dim Wb As Workbook Dim iSht As Worksheet Dim rSht As Worksheet Dim pSht ...
 - 最长上升子序列 nlogn
			
; LL num[N]; LL dp[N]; LL go(LL l, LL r, LL k) { for (; r >= l; r--) if (dp[r] <= k) return r; ...
 - IntelliJ IDEA 第一个 Scala 程序
			
IntelliJ 安装完成 Scala 插件后,你需要尝试使用 IntelliJ 来创建并且运行第一个程序. 通常这个程序只是简单的输出 Hello World. 创建一个新工程 在文件下面选择新建, ...
 - 解决无法安装Microsoft .Net Framework 3.5
			
如果解决不了,试试我的方法吧,我也在网上找了好久,最终在本地解决了 所需工具:dism,和Net Framework 3.5,已经打包 链接:https://pan.baidu.com/s/1nKok ...