Skip to content
Browse files

Reorganize static assets and forms sections.

  • Loading branch information...
1 parent d1918e1 commit 877ad2b2a5f6e6c30c0fb055777d99de00d93306 @mikeorr mikeorr committed Feb 20, 2012
Showing with 80 additions and 5 deletions.
  1. +65 −0 forms/file_uploads.rst
  2. +6 −0 forms/index.rst
  3. +2 −5 index.rst
  4. 0 { → static_assets}/files.rst
  5. +7 −0 static_assets/index.rst
View
65 forms/file_uploads.rst
@@ -0,0 +1,65 @@
+File Uploads
+%%%%%%%%%%%%
+
+There are two parts necessary for handling file uploads. The first is to
+make sure you have a form that's been setup correctly to accept files. This
+means adding ``enctype`` attribute to your ``form`` element with the value of
+``multipart/form-data``. A very simple example would be a form that accepts
+an mp3 file. Notice we've setup the form as previously explained and also
+added an ``input`` element of the ``file`` type.
+
+.. code-block:: html
+ :linenos:
+
+ <form action="/store_mp3_view" method="post" accept-charset="utf-8"
+ enctype="multipart/form-data">
+
+ <label for="mp3">Mp3</label>
+ <input id="mp3" name="mp3" type="file" value="" />
+
+ <input type="submit" value="submit" />
+ </form>
+
+The second part is handling the file upload in your view callable (above,
+assumed to answer on ``/store_mp3_view``). The uploaded file is added to the
+request object as a ``cgi.FieldStorage`` object accessible through the
+``request.POST`` multidict. The two properties we're interested in are the
+``file`` and ``filename`` and we'll use those to write the file to disk.
+
+.. code-block:: python
+ :linenos:
+
+ import os
+ from pyramid.response import Response
+
+ def store_mp3_view(request):
+ # ``filename`` contains the name of the file in string format.
+ #
+ # WARNING: this example does not deal with the fact that IE sends an
+ # absolute file *path* as the filename. This example is naive; it
+ # trusts user input.
+
+ filename = request.POST['mp3'].filename
+
+ # ``input_file`` contains the actual file data which needs to be
+ # stored somewhere.
+
+ input_file = request.POST['mp3'].file
+
+ # Using the filename like this without cleaning it is very
+ # insecure so please keep that in mind when writing your own
+ # file handling.
+ file_path = os.path.join('/tmp', filename)
+ output_file = open(file_path, 'wb')
+
+ # Finally write the data to the output file
+ input_file.seek(0)
+ while 1:
+ data = input_file.read(2<<16)
+ if not data:
+ break
+ output_file.write(data)
+ output_file.close()
+
+ return Response('OK')
+
View
6 forms/index.rst
@@ -0,0 +1,6 @@
+Forms
+%%%%%
+
+.. toctree::
+
+ file_uploads
View
7 index.rst
@@ -11,19 +11,16 @@ The Pyramid Cookbook presents topical, practical usages of :mod:`Pyramid`.
database/index
debugging
deployment/index
+ forms/index
logging/index
porting/index
pylons/index
routing/index
+ static_assets/index
templates/index
testing/index
views/index
misc/index
-
-.. toctree::
- :maxdepth: 2
-
- files
glossary
todo
View
0 files.rst → static_assets/files.rst
File renamed without changes.
View
7 static_assets/index.rst
@@ -0,0 +1,7 @@
+Static Assets (Static Files)
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+
+.. toctree::
+ :maxdepth: 2
+
+ files

0 comments on commit 877ad2b

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