Skip to content
This repository
Browse code

send_from_directory > SharedDataMiddleware

  • Loading branch information...
commit f1a6fbe250ccf5407b45a398d05bf63bc99c3105 1 parent 16e4d5a
Armin Ronacher authored July 06, 2010

Showing 1 changed file with 15 additions and 8 deletions. Show diff stats Hide diff stats

  1. 23  docs/patterns/fileuploads.rst
23  docs/patterns/fileuploads.rst
Source Rendered
@@ -28,8 +28,6 @@ bootstrapping code for our application::
28 28
     ALLOWED_EXTENSIONS = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'])
29 29
 
30 30
     app = Flask(__name__)
31  
-    app.add_url_rule('/uploads/<filename>', 'uploaded_file',
32  
-                     build_only=True)
33 31
 
34 32
 So first we need a couple of imports.  Most should be straightforward, the
35 33
 :func:`werkzeug.secure_filename` is explained a little bit later.  The
@@ -100,14 +98,23 @@ before storing it directly on the filesystem.
100 98
    >>> secure_filename('../../../../home/username/.bashrc')
101 99
    'home_username_.bashrc'
102 100
 
103  
-Now if we run that application, you will notice that uploading works, but
104  
-you won't actually see that uploaded file.  Well, you would have to
105  
-configure the server to serve that file for you.  This is not handy for
106  
-development situations or when you are just too lazy to properly set up
107  
-the server.  Would be nice to have the files still be available in that
108  
-situation, and that is really easy to do, just hook in a middleware::
  101
+Now one last thing is missing: the serving of the uploaded files.  As of
  102
+Flask 0.5 we can use a function that does that for us::
  103
+
  104
+    from flask import send_from_directory
  105
+
  106
+    @app.route('/uploads/<filename>')
  107
+    def uploaded_file(filename):
  108
+        return send_from_directory(app.config['UPLOAD_FOLDER'],
  109
+                                   filename)
  110
+
  111
+Alternatively you can register `uploaded_file` as `build_only` rule and
  112
+use the :class:`~werkzeug.SharedDataMiddleware`.  This also works with
  113
+older versions of Flask::
109 114
 
110 115
     from werkzeug import SharedDataMiddleware
  116
+    app.add_url_rule('/uploads/<filename>', 'uploaded_file',
  117
+                     build_only=True)
111 118
     app.wsgi_app = SharedDataMiddleware(app.wsgi_app, {
112 119
         '/uploads':  UPLOAD_FOLDER
113 120
     })

0 notes on commit f1a6fbe

Please sign in to comment.
Something went wrong with that request. Please try again.