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))

      问题解决。

    实践出真知~
  • 相关阅读:
    hdu 4777 树状数组+合数分解
    hdu5635 BestCoder Round #74 (div.2)
    hdu 5636 搜索 BestCoder Round #74 (div.2)
    hdu 5637 BestCoder Round #74 (div.2)
    hdu4605 树状数组+离散化+dfs
    hdu4521 线段树+dp
    hdu3340 线段树+多边形
    孜孜不倦,必能求索;风尘仆仆,终有归途。
    增加、删除类文件或者在一个类中增加、删除方法时,是不能够热部署到服务上的。这时候需要停止服务器重新部署后再启动,就不会出现上面的提示了。
    为什么jdk1.8不支持sql.append,该如何解决
  • 原文地址:https://www.cnblogs.com/NolaLi/p/10144905.html
Copyright © 2011-2022 走看看