Flask文件上传limi

2024-06-02 08:20:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个用于多个文件上载的文件上载处理程序,并设置了最大内容大小。文档中提到,当文件总大小超过限制时,Flask抛出413异常,因此我还编写了一个413错误处理程序,其中包含一个自定义413页。但是,在测试文件上传时,我可以看到413错误确实被抛出了,但是每次连接似乎都会中断,而不是呈现我的错误页面。仅供参考,我目前正在使用Flask dev服务器。

代码:

app.config['MAX_CONTENT_LENGTH'] = 50 * 1024 * 1024    # 50 Mb limit

@app.route('/upload', methods=['POST'])
def upload_files():
    if request.method == 'POST':
       uploaded_files = request.files.getlist('uploaded_files[]')

       # do some stuff with these files



@app.errorhandler(413)
def error413(e):
    return render_template('413.html'), 413

更新:

很奇怪,这个问题似乎只在使用Flask dev服务器时出现。我在Apache上测试它,我的413错误页面显示良好。


Tags: 文件文档devapp处理程序flask内容request