简体   繁体   中英

Cannot upload files bigger than 1MB from JavaScript to Flask

When I select small files there is no problem. But for big files larger than around 1MB I get Failed to load resource:.net::ERR_CONNECTION_RESET on Chromium or NetworkError when attempting to fetch resource on Firefox console and 308 status code on server, whether I use the production server or Gunicorn. Any solution?

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

EDIT: I found the problem. The problem is the query string. The following codes work. Now I want to know how can I pass the query string using PUT or POST method to Flask?

New JavaScript:

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

New Python:

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

OK. Here is the solution. Just watch out for slashes:

JavaScript code:

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

Python code:

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

But I do not know why.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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