1

I am developing an app using Flask, In the app, user upload files to the server, before uploading I used to check that user is authenticated or not. If the user is authenticated, then the uploading file is saved in the server otherwise flask redirected to home page.

Code - In app.py -

@login_required
@app.route('/home')
def home():
    if current_user.Is_Authenticated:
        return redirect(url_for('post1'))
    else:
        return render_template('post2.html')

@app.route('/upload', methods=['POST'])
def upload():
    if current_user.Is_Authenticated:
        user = current_user.Email
        flag = True
    else:
        print("Current User is not Authenticated")
        flag = False
    if(flag):
        if(request.method == "POST"):
            if(request.files['myfile']):
                myfile = request.files["myfile"]
                
                sfname = os.path.join(os.getcwd(), 'static', str(secure_filename(myfile.filename)))
                myfile.save(sfname)
                return render_template('post.html')
    else:
        return redirect(url_for('home'))

When, I am testing the app, I found that, before authenticated, a user upload files with larger than 10MB, it shows "Site is not reachable". If it is small sized file, then flask redirected to home page, correctly.

how to solve this, why flask is not working when the uploading file size is larger than 10Mb, Thank you

  • I expect you don't want to put the @login_required decorator on the home path if you plan to check for unauthenticated users in the handler. Maybe add it to the upload endpoint and remove the inline auth checks in upload? – user650881 Jan 08 '21 at 08:52
  • 1
    Try this `app.config['MAX_CONTENT_LENGTH'] = 16 * 1000 * 1000 ` where 16 means 16 megabytes change it to 100 and see if it makes any difference https://flask.palletsprojects.com/en/master/patterns/fileuploads/#:~:text=New%20in%20version%200.6.&text=The%20code%20above%20will%20limit,will%20raise%20a%20RequestEntityTooLarge%20exception. – Ahmad Karim Jan 08 '21 at 08:55
  • @AhmadKarim Sir, I tried setting to 100Mb, still not working when I upload the file greater than 10Mb, but files with less size is uploaded correctly, Is there any other solution, thanks – Karthiyayini Naga Jan 08 '21 at 09:38
  • @user650881, I removed the login decorator, still shows the same... – Karthiyayini Naga Jan 08 '21 at 09:40
  • @KarthiyayiniNaga if your problem is not solved yet you can try [this](https://stackoverflow.com/questions/44727052/handling-large-file-uploads-with-flask). – Chandan Jan 16 '21 at 14:49

1 Answers1

0

In Flask you can limit the file size using MAX_CONTENT_LENGTH while configuring your app.

If the file size is greater than MAX_CONTENT_LENGTH you get this kind of errors Please refer to this (for error messages, for different browser and there are also solutions as well)

Increase this MAX_CONTENT_LENGTH to handle large file size

or use the below code to handle large file error

@app.errorhandler(413)
def request_entity_too_large(error):
    return 'File Too Large', 413

Happy coding!

Abhinava B N
  • 165
  • 1
  • 12