Setting File Upload Size Limits in Flask
In this tutorial, we will learn how to set Flask’s upload file size limit.
Setting up the Flask Application
Initially, we need to import the Flask
and render_template
libraries from the Flask module. Next, we initialize the Flask application with the app
variable. After that, we create an index function for the home routing page.
from flask import Flask, request, redirect, url_for,render_template app = Flask(__name__) @app.route('/') def index(): return render_template('index.html') if __name__ == '__main__': app.run(debug=True)
The function render_template
displays the index.html page which looks like this for instance:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>MyPage</title> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.4.1/dist/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous"> <link rel="stylesheet" href="../static/css/style.css"> </head> <body> <h1 class="main-heading"> File Upload </h1> <form action="/upload" method="post" enctype="multipart/form-data"> <div class="my-div"> <div class="mb-3"> <label for="formFileLg" class="form-label">Upload a file of size limit 50kB...</label> <input class="form-control form-control-lg" id="formFileLg" type="file" name="file"> </div> <button type="submit" class="btn btn-primary">Upload</button> </div> </form> </body> </html>
This is how the website looks:
Set file upload size – Limit
The MAX_CONTENT_LENGTH
configuration in Flask is used to specify the largest size that uploaded files can have.
from flask import Flask, request,render_template from werkzeug.exceptions import RequestEntityTooLarge app = Flask(__name__) app.config['MAX_CONTENT_LENGTH']=50*1024 # 50*1024 bytes=50 Kb @app.route('/') def index(): return render_template('index.html') @app.route('/upload',methods=['POST']) def upload_file(): try: file=request.files['file'] if file: file.save(f'uploads/{file.filename}') return '<h1>File uploaded successfully!!..</h1>' else: return '<h1 style="color:red">File not attached!!..</h1>' except RequestEntityTooLarge: return '<h1 style="color:red">File size exceeded!!..</h1>' if __name__ == '__main__': app.run(debug=True)
- Setting
MAX_CONTENT_LENGTH
:app.config['MAX_CONTENT_LENGTH']
sets the maximum allowed payload size to n-bytes (if size is 50Mb,then size will be 50*1024*1024 bytes). - Handling File Uploads: The
/upload
route handles file uploads. It checks if the file part is in the request and if a file is selected, the uploaded files are finally saved in the uploads folder. - Exception Handling: The try-except block handles the RequestEntityTooLarge exception from
werkzeug.exceptions
.
Examples
Let’s look at a few examples of how we can limit the file size:
Case 1: File size is less than the maximum permissible size
Case 2:File size is more than the maximum permissible size
Also read: Redirection in Flask
Leave a Reply