Skip to content
Myr

Documented MAX_CONTENT_LENGTH in the file upload docs and linked to Flask-Uploads

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Armin Ronacher · committed by Armin Ronacher · parent 4aeb445 · Browse files · 1 file+20 added−7 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+20 added−7 removed

  • other:+20 added−7 removed

1 file+20 added−7 removed

modified:docs/patterns/fileuploads.rst+20 added-7 removed
123 unmodified lines
124125Improving Uploads126-----------------127128.. versionadded:: 0.6129130So how exactly does Flask handle uploads? Well it will store them in the131webserver's memory if the files are reasonable small otherwise in a132temporary location (as returned by :func:`tempfile.gettempdir`). But how133do you specify the maximum file size after which an upload is aborted? By134default Flask will happily accept file uploads to an unlimited amount of133memory, but you can limit that by subclassing the request and overriding134the Werkzeug provided :attr:`~werkzeug.BaseRequest.max_form_memory_size`135attribute::135memory, but you can limit that by setting the `MAX_CONTENT_LENGTH`136config key::137138 from flask import Flask, Request139139 class LimitedRequest(Request):140 max_form_memory_size = 16 * 1024 * 1024141140 app = Flask(__name__)143 app.request_class = LimitedRequest141 app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024142143The code above will limited the maximum allowed payload to 16 megabytes.144If a larger file is transmitted, Flask will raise an145:exc:`~werkzeug.exceptions.RequestEntityTooLarge` exception.146147This feature was added in Flask 0.6 but can be achieved in older versions148as well by subclassing the request object. For more information on that149consult the Werkzeug documentation on file handling.150151152Upload Progress Bars153--------------------154155A while ago many developers had the idea to read the incoming file in
10 unmodified lines
166167- `Plupload <http://www.plupload.com/>`_ - HTML5, Java, Flash168- `SWFUpload <http://www.swfupload.org/>`_ - Flash169- `JumpLoader <http://jumploader.com/>`_ - Java170171172An Easier Solution173------------------174175Because the common pattern for file uploads exists almost unchanged in all176applications dealing with uploads, there is a Flask extension called177`Flask-Uploads`_ that implements a full fledged upload mechanism with178white and blacklisting of extensions and more.179180.. _Flask-Uploads: http://packages.python.org/Flask-Uploads/