Want to see it live ?

Notice that for this page, the max upload size is set to 2MB.

Load jQuery

Before anything, to use the whole feature of the plugin, we need to import jQuery:

<script type="text/javascript"
        src="/%(SCRIPT_NAME)s/gp.fileupload.static/jquery.js"></script>

In our case, jQuery is already provided by Sphinx so we don’t need to include it twice.

This is an existing html form


Here is the code:

<form method="POST"
      enctype="multipart/form-data"
      action="./demo.html">
  <input type="file" name="file1" />
  <br />
  <input type="submit" />
</form>

This form is automatically bound with the jquery.fileupload.auto.js script so we don’t need any javascript code.

It is totally transparent for your application code since the result page is your application’s result page

This is a form generated with the fileUpload plugin

Here is the code:

<div id="forms"></div>

<script language="javascript">
  jQuery(document).ready(function() {
      jQuery('#forms').fileUpload(
            {action:'./demo.html',
             success:function(){alert('Yeah !')}});
  });
</script>

This is a static page. How does it work???!!!

This page must be served by a paste.urlparser.StaticURLParser app. Here is the code used to consume the wsgi.input when needed:

>>> class FileUploadDemo(object):
...     """Wrap a `paste.urlparser.StaticURLParser` to consume stdin if needed
...     """
...     def __init__(self, application):
...         self.application = application
...     def __call__(self, environ, start_response):
...         if environ['REQUEST_METHOD'] == 'POST':
...             if 'gp.fileupload.id' in environ['QUERY_STRING']:
...                 # need to consume so see how many block we have to read
...                 bsize = 1024
...                 length = int(environ['CONTENT_LENGTH'])
...                 blocks = [bsize for i in range(bsize, length, bsize)]
...                 blocks.append(length-len(blocks)*bsize)
...                 # read input an write to /dev/null :)
...                 rfile = environ['wsgi.input']
...                 [rfile.read(size) for size in blocks]
...         # StaticURLParser only serve GET
...         environ['REQUEST_METHOD'] = 'GET'
...         return self.application(environ, start_response)

And the application factory:

>>> from paste.urlmap import URLMap
>>> from paste.urlparser import StaticURLParser
>>> from gp.fileupload import make_app as make_fileupload

>>> def make_app(global_conf, **kw):
...     map = URLMap({})
...     app = StaticURLParser('/where/is/my/doc')
...     map['/gp.fileupload'] = make_fileupload(
...                               FileUploadDemo(app),
...                               global_conf,
...                               tempdir=kw.get('tempdir'),
...                               max_size='2',
...                               include_files=['fileupload.css',
...                                              'jquery.fileupload.*'])
...     return map

Table Of Contents

Previous topic

Download

Next topic

Contributors

This Page