Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从python flask中的另一个文件调用send_file函数时出错| RuntimeError:在请求上下文之外工作_Python_Flask_Sendfile - Fatal编程技术网

从python flask中的另一个文件调用send_file函数时出错| RuntimeError:在请求上下文之外工作

从python flask中的另一个文件调用send_file函数时出错| RuntimeError:在请求上下文之外工作,python,flask,sendfile,Python,Flask,Sendfile,问候stack overflow社区,我目前正在开发一个flask应用程序,我正在尝试使用flask中的send_file方法从助手函数检索一个文件 我有一条路线是这样的: @app.route("/process",methods=['GET','POST']) def do_something(): process = threading.Thread(target=function_name,args=[arg1,arg2]) process.start() ret

问候stack overflow社区,我目前正在开发一个flask应用程序,我正在尝试使用flask中的send_file方法从助手函数检索一个文件

我有一条路线是这样的:

@app.route("/process",methods=['GET','POST'])
def do_something():
    process = threading.Thread(target=function_name,args=[arg1,arg2])
    process.start()
    return render_template("template.html")
使用
函数\u name
(位于另一个文件上)函数返回如下文件

def function_name():
        filename = 'ohhey.pdf'
        return send_file(filename,as_attachment=True,cache_timeout=0)
当我像这样运行我的应用程序时,我得到以下错误

RuntimeError: Working outside of application context.

This typically means that you attempted to use functionality that needed
to interface with the current application object in some way. To solve
this, set up an application context with app.app_context().  See the
documentation for more information.
因此,我尝试更改以下函数:

def function_name():
        filename = 'ohhey.pdf'
        with app.app_context():
            return send_file(filename,as_attachment=True,cache_timeout=0)

def function_name():
        filename = 'ohhey.pdf'
        with app.test_request_context():
            return send_file(filename,as_attachment=True,cache_timeout=0)
然后得到这个新的错误

RuntimeError: Working outside of request context.

This typically means that you attempted to use functionality that needed
an active HTTP request.  Consult the documentation on testing for
information about how to avoid this problem.
因此,我尝试以下方法:

def function_name():
        filename = 'ohhey.pdf'
        with app.app_context():
            return send_file(filename,as_attachment=True,cache_timeout=0)

def function_name():
        filename = 'ohhey.pdf'
        with app.test_request_context():
            return send_file(filename,as_attachment=True,cache_timeout=0)
在进行最后更改后,我的应用程序不会返回文件或错误。我感谢你的帮助