博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
flask下载zip文件报错TypeError
阅读量:4618 次
发布时间:2019-06-09

本文共 2973 字,大约阅读时间需要 9 分钟。

  报错内容: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/
') 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_filedirectory = 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))

 

  问题解决。

 

转载于:https://www.cnblogs.com/NolaLi/p/10144905.html

你可能感兴趣的文章
hdu 1085 Holding Bin-Laden Captive!
查看>>
[周记]8.7~8.16
查看>>
递归定义
查看>>
kindeditor 代码高亮设置
查看>>
图的邻接表存储
查看>>
2018 leetcode
查看>>
PHP中获取当前页面的完整URL
查看>>
所谓输入掩码技术,即只有数字键起作用
查看>>
Display对象,Displayable对象
查看>>
安装oracle11G,10G时都会出现:注册ocx时出现OLE初始化错误或ocx装载错误对话框
查看>>
生产环境下正则的应用实例(一)
查看>>
在CentOS7命令行模式下安装虚拟机
查看>>
Arduino可穿戴开发入门教程Arduino开发环境介绍
查看>>
Windows平台flex+gcc词法分析实验工具包
查看>>
3.Python基础 序列sequence
查看>>
Chapter 4 Syntax Analysis
查看>>
vi/vim使用
查看>>
讨论Spring整合Mybatis时一级缓存失效得问题
查看>>
Maven私服配置Setting和Pom文件
查看>>
Linux搭建Nexus3.X构建maven私服
查看>>