Skip to content
Myr

Merge remote-tracking branch 'sourya/master'

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

authored by Markus Unterwaditzer · committed by Markus Unterwaditzer · parents 818c42c, a6ea633 · Browse files · 1 file+11 added−15 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+11 added−15 removed

  • other:+11 added−15 removed

1 file+11 added−15 removed

modified:docs/patterns/fileuploads.rst+11 added-15 removed
31 unmodified lines
3233So first we need a couple of imports. Most should be straightforward, the34:func:`werkzeug.secure_filename` is explained a little bit later. The35``UPLOAD_FOLDER`` is where we will store the uploaded files and the36``ALLOWED_EXTENSIONS`` is the set of allowed file extensions. Then we add a37URL rule by hand to the application. Now usually we're not doing that, so38why here? The reasons is that we want the webserver (or our development39server) to serve these files for us and so we only need a rule to generate40the URL to these files.36``ALLOWED_EXTENSIONS`` is the set of allowed file extensions.3738Why do we limit the extensions that are allowed? You probably don't want39your users to be able to upload everything there if the server is directly40sending out the data to the client. That way you can make sure that users
62 unmodified lines
103104 >>> secure_filename('../../../../home/username/.bashrc')105 'home_username_.bashrc'106111Now one last thing is missing: the serving of the uploaded files. As of112Flask 0.5 we can use a function that does that for us::107Now one last thing is missing: the serving of the uploaded files. In the 108:func:`upload_file()` we redirect the user to 109``url_for('uploaded_file', filename=filename)``, that is, ``/uploads/filename``. 110So we write the :func:`uploaded_file` function to return the file of that name. As111of Flask 0.5 we can use a function that does that for us::112113 from flask import send_from_directory114115 @app.route('/uploads/<filename>')
51 unmodified lines
167client asks the server every 5 seconds how much it has transmitted168already. Do you realize the irony? The client is asking for something it169should already know.170172Now there are better solutions to that work faster and more reliable. The173web changed a lot lately and you can use HTML5, Java, Silverlight or Flash174to get a nicer uploading experience on the client side. Look at the175following libraries for some nice examples how to do that:171Now there are better solutions that work faster and are more reliable. There 172are JavaScript libraries like jQuery (http://jQuery.com) that have form plugins 173to ease the construction of progress bar. Another great option is dropzone.js 174(http://www.dropzonejs.com) that allows users to drag and drop files on to the 175page.176177- `Plupload <http://www.plupload.com/>`_ - HTML5, Java, Flash178- `SWFUpload <http://www.swfupload.org/>`_ - Flash179- `JumpLoader <http://jumploader.com/>`_ - Java177181178An Easier Solution179------------------180181Because the common pattern for file uploads exists almost unchanged in all
5 unmodified lines