首页 > 解决方案 > 如何上传大(~100Mb)文件?

问题描述

我有以下烧瓶应用程序

文件格式.html:

<html>
    <head>
        <title>Simple file upload using Python Flask</title>
    </head>
    <body>
        <form action="/getSignature" method="post" enctype="multipart/form-data">
          Choose the file: <input type="file" name="photo"/><BR>
              <input type="submit" value="Upload"/>
        </form>
    </body>
</html>

应用程序.py:

import os
from flask import Flask, request, render_template, url_for, redirect


app = Flask(__name__)


@app.route("/")
def fileFrontPage():
    return render_template('fileform.html')

@app.route("/getSignature", methods=['POST'])
def handleFileUpload():
    if 'photo' in request.files:
        photo = request.files['photo']
        if photo.filename != '':
            filepath = os.path.join('/flask/files', photo.filename)
            photo.save(filepath)
    return render_template('result.html')

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000)

这适用于较小的文件,但不适用于较大的文件。点击上传按钮后,浏览器显示(Uploading 13%..),然后浏览器超时ERR_CONNECTION_RESET。在烧瓶应用程序中看不到任何错误。

我通过 Nginx 提供服务。当我检查 nginx 日志时,我看到

2020/02/20 22:38:47 [error] 6#6: *170614 client intended to send too large body: 80762097 bytes, client: 10.2.16.178, server: localhost, request: "POST /getSignature

我需要为此添加任何 Nginx 配置吗?

我想上传最大 100Mb 的文件。

标签: pythonflask

解决方案


内容长度可能被限制为默认值。

当您使用 nginx 时,可能是它玩游戏的上游超时(默认为 upstream keepalive_timeout 60s)将以下设置添加到 nginx:

keepalive_timeout 900s;

#extra
proxy_read_timeout 900s;
uwsgi_read_timeout 900s;

更多调音在这里

如果你使用像 uwsgi 这样的 python wsgi 服务器,还要在那里设置keep alive

http-keepalive 900;

推荐阅读