0

I am setting a file limit in Flask. When files are uploaded it correctly blocks files over 4mb and returns a 413 error. However this is closing the connection on the server which means that the redirect doesn't work. What I want to do is keep the connection open on this error so I can then redirect the user.

My file size limit is set as:

app.config['MAX_CONTENT_LENGTH'] = 4 * 1024 * 1024

The code which captures the exception is:

try:
    form = FileUploadForm();
except Exception as e:
    print(e)
    flash(u"File size too large: please choose a file under 4mb","danger")
    return redirect(url_for("home"))

This is the exception returned:

413 Request Entity Too Large: The data value transmitted exceeds the capacity limit.

The code "return redirect(url_for("home"))" doesn't work, I believe because the connection is closed on error 413. This is Flask running in development mode.

I have tried the code in the suggested answer, but that doesn't seem to work. My code now looks like:

@app.errorhandler(413)
def largefile_error(e):
    print("Large file")
    return redirect(url_for("addsign")), 413

@app.route('/addsign', methods=['GET', 'POST'])
def addsign():
    if not current_user.is_authenticated:
        flash(f"Please login to upload a sign","danger")
        return redirect(url_for('home'))
    form = FileUploadForm()

In the console I can see the following output.

Large file
127.0.0.1 - - [13/Jul/2020 18:02:34] "POST /addsign HTTP/1.1" 413 -
OptimusPrime
  • 777
  • 16
  • 25
  • If I understand HTTP correctly, this is not possible - you have either to consume the full upload and then return something OR close the connection. – Christian Sauer Jul 13 '20 at 06:05
  • Is there a way to return a different error than 413 and then allow a redirect? Do you have an example of consuming the full upload? – OptimusPrime Jul 13 '20 at 06:25

1 Answers1

0
from flask import redirect

@app.errorhandler(413)
def largefile_error(e):
 return redirect(url_for("your_function_name")), 413

you can use this for reference

M_x
  • 782
  • 1
  • 8
  • 26
  • Thanks, I will try this later, but will it work given that error 413 causes the connection to close? I am not sure if this will apply before the connection closes or after. From the document page, I can see that it says aborts early so hopefully that will work. – OptimusPrime Jul 13 '20 at 08:08
  • ya it will WORK. `413` is for a ERROR `Request Entity Too Large`. error handler is a function that returns a response when a type of error is raised -> [https://flask.palletsprojects.com/en/1.1.x/patterns/errorpages/#error-handlers] – M_x Jul 13 '20 at 08:23
  • 1
    Sorry, this doesn't work, I can see that the largefile_error method is being accessed, but the connection is still reset. – OptimusPrime Jul 13 '20 at 16:53
  • may be server don't want user to continue upload file so server is closing connection – M_x Jul 14 '20 at 04:34
  • checkout this [https://stackoverflow.com/a/7597257] , [https://github.com/pallets/flask/pull/2662]. i think it is major issue with dev servers – M_x Jul 14 '20 at 04:34
  • 1
    I used JQuery to validate the file field on change so that if the file is too big it bounces back with an error and prompts the user to select a different file. – OptimusPrime Jul 15 '20 at 07:54