zoukankan      html  css  js  c++  java
  • 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))

      问题解决。

    实践出真知~
  • 相关阅读:
    磁盘冗余阵列之RAID 5
    磁盘冗余阵列之RAID10
    Linux常见的命令与vi的介绍
    通过挂在系统光盘搭建本地yum
    Windows 2008 系统安装
    在windows主机中,利用XSHELL生成“密钥”进行虚拟机与物理机的传输
    在VMware下进行的使用ssh服务管理远程主机
    Linux中的快捷方式
    Linux中常用命令
    Linux中vi命令的详细总结
  • 原文地址:https://www.cnblogs.com/NolaLi/p/10144905.html
Copyright © 2011-2022 走看看