繁体   English   中英

无法将大于 1MB 的文件从 JavaScript 上传到 Flask

[英]Cannot upload files bigger than 1MB from JavaScript to Flask

当我select小文件时没有问题。 但是对于大于 1MB 左右的大文件,无论我使用生产服务器还是 Gunicorn,我都会在 Firefox 控制台上NetworkError when attempting to fetch resource在 Chromium 或 NetworkError 上Failed to load resource:.net::ERR_CONNECTION_RESET和服务器上的308状态代码。 任何解决方案?

JavaScript:

let data = new FormData();
data.append("file", inputFile.files[0]);
let response = await fetch("/upload?key=value", {method: "PUT", body: data});

Python:

@app.put('/upload/')
def upload_file():
    key = request.args['key']
    file = request.files['file']
    print(key)
    print(file)
    return ''

编辑:我发现了问题。 问题是查询字符串。 以下代码有效。 现在我想知道如何使用 PUT 或 POST 方法将查询字符串传递给 Flask?

新 JavaScript:

let data = new FormData();
data.append("file", inputFile.files[0]);
let response = await fetch("/upload/", {method: "PUT", body: data});

新 Python:

@app.put('/upload/')
def upload_file():
    file = request.files['file']
    print(file)
    return ''

好的。 这是解决方案。 请注意斜杠:

JavaScript 代码:

let data = new FormData();
data.append("file", inputFile.files[0]);
let response = await fetch("/upload?key=value", {method: "PUT", body: data});

Python 代码:

@app.put('/upload') # no slash at the end
def upload_file():
    file = request.files['file']
    value = request.args['key']
    print(file)
    print(value)
    return ''

但我不知道为什么。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM