1

I'm trying to convert my Flask server to Quart.

I have a form with file inputs in it, which I send over to the server. It used to work perfectly with Flask, but now I fail to send the files over.

Here's the code which worked:

app = Flask(__name__)

@app.route('/upload', methods=('POST',))
def process_form_data():
    for name, file in request.files.items():
        print(f'Processing {name}: {len(file.read())}')
    return make_response(jsonify({"message": "File uploaded"}), 200)

The sending code is:

const request = new XMLHttpRequest();
function check_and_post(e) {
    const formElement = e.target;
    request.addEventListener("load", function (e) {
        if (request.status == 200) {
          window.location.href = '/work';
        }
        else {
          alert('Error uploading file');
          console.log(e);
        }
    });
    // request error handler
    request.addEventListener("error", function (e) {
        alert('Error uploading files, try again.');
    });
    request.open("post", formElement.action);
    request.responseType = "json";
    request.send(new FormData(formElement));
}

My flask code consumed the files, and the load event was received with status 200.

My Quart code is as follows:

app = Quart(__name__)

@app.route('/upload', methods=('POST',))
async def process_form_data():
    for name, file in (await request.files).items():
        print(f'Processing {name}: {len(file.read())}')
    return make_response(jsonify({"message": "File uploaded"}), 200)

Now I get to the "Error uploading file" branch, i.e. the request status received for the progress event isn't 200 (I didn't get any request for the error event).

The status of the request I now get in load is 413 (request too large?). The server side does not log any exception or warning.

Any idea?

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
feature_engineer
  • 1,088
  • 8
  • 16
  • This was also asked on the [Quart issue tracker](https://gitlab.com/pgjones/quart/issues/281). The suggestion is to adjust the `MAX_CONTENT_LENGTH` configuration option. – pgjones Dec 06 '19 at 16:32

1 Answers1

0

just in case someone lese search for this: Quart has a configurable property that let you to set the maximum size of your requests payload (MAX_CONTENT_LENGTH) to set that use the below command in

max_mb= 50 app.config['MAX_CONTENT_LENGTH'] = mb *1000 * 1024 # for 50 megabyte

Mehran
  • 11
  • 6