Package: gp.fileupload
TheSphinxversion of this documentation can be foundhere.Ademopage is also
available.If you found a bug submit anissue <http://www.bitbucket.org/gawel/gpfileupload/issues/?status=new>.ContentsDescriptionUpload middlewareMiddlewareApplication codeStorage middlewareUsagePaste factoriesAjax stuffDescriptionExamplesContributorsNews1.2 (2012-09-03)1.11.00.9 (2010-03-20)0.8 (2009-02-20)0.7 (2009-02-08)0.6 (2008-11-24)0.5 (2008-08-28)0.4 (2008-08-12)0.3 (2008-07-27)0.2 (2008-07-25)0.1 (2008-07-24)Descriptiongp.fileuploadis a set of wsgi middleware to deal with large file upload.gp.fileupload.FileUploadis a wsgi middleware to get the stat of large
files uploaded to the server. Optional javascript code is provided to animate
a progress bar.gp.fileupload.Storageis a wsgi middleware to store files on file system
and avoid big transaction in your application.Upload middlewareThe principle is to count uploaded chunks in the wsgi loop, transmit them to the
application, and provide a link with up-to-date json information. The optional
javascript code is able to use an existing form and replace it with a progress
bar during upload. It can also generate its own upload form, then upload several
files sequentially with multiple progress bars. gp.fileupload can be used
without modifying your application, just by adding it in the wsgi stack.It has currently been tested with Pylons and Zope 3.MiddlewareWrap your wsgi application with the middleware:>>> from gp.fileupload import FileUpload

>>> def my_application(environ, start_response):
...     start_response('200 OK', [('Content-Type', 'txt/html')])
...     return ['<html><body>My app</body></html>']

>>> app = FileUpload(my_application, tempdir=TEMP_DIR,
...                  max_size=None)

>>> def application(environ, start_response):
...     return app(environ, start_response)TheFileUploadmiddleware has the following options:tempdir: A path to a temporary foldermax_size: Max allowed size. If the file size is larger thanmax_sizeaRuntimeErroris raised.include_files: A list of static files that the middleware should include in
your html body (see below). You might want to include the files by yourself
in the relevant pages, or they will be included in all of your pages.require_session: a boolean flag that disable the middleware for
requests that don’t provide the SESSION_name in the query.Application codeWrite an html form like this:<form enctype="multipart/form-data"
      method="POST"
      action=".?gp.fileupload.id=1">
  <input type="file" name="file" />
  <input type="submit" />
</form>Where 1 is the session id. The session idmustbe a digit.When the form is submitted, you can use some ajax stuff to get the stats of the upload with the url:http://yourhost/gp.fileupload.stat/1This will return some JSON data like:{'state': 1, 'percent': 69}statecan have the following values:0: nothing done yet.1: upload is active-1: file is larger than max_size.You can use this to display the upload progress.Storage middlewareThe storage middleware provide a way to avoid long transaction in your application.POST content is written to a temporary directory. If the POST contains some
files then the files are moved to the storage directory. The files content is
replaced by the path of the real file relative to the storage root.This way, your application receivealwaysa few Ko of data.UsageWrap your wsgi application with the middleware:>>> from gp.fileupload import Storage
>>> from gp.fileupload import purge_files
>>> import cgi

>>> def my_application(environ, start_response):
...     """simple app to read the file path from the request and remove it
...     from the storage directory
...     """
...     if environ['REQUEST_METHOD'] == 'POST':
...         fields = cgi.FieldStorage(fp=environ['wsgi.input'],
...                                   environ=environ,
...                                   keep_blank_values=1)
...         relative_path = fields['file'].read()
...         # remove file from storage
...         purge_files(environ, relative_path)
...     start_response('200 OK', [('Content-Type', 'txt/html')])
...     return ['<html><body>My app</body></html>']

>>> app = Storage(my_application,
...               upload_to='/tmp/share/files',
...               tempdir='/tmp/upload_tmp',
...               )

>>> def application(environ, start_response):
...     return app(environ, start_response)TheStoragemiddleware has the following options:upload_to: the root directory of the storage tree.tempdir: A path to a temporary folderexclude_paths: a list of regular expressions. All matchedPATH_INFOwill
be ignored.  If this parameters is not None, then the middleware will also
catch non-HTML content send by your application. WARNING: This options is
experimental and not well tested.require_session: a boolean flag that disable the middleware for
requests that don’t provide the SESSION_name in the query.Paste factoriesThe package provides a filter factory usable inPasteDeployconfiguration files.The factory provides the middleware itself:[pipeline:main]
pipeline = fileupload egg:myapp

[filter:fileupload]
use = egg:gp.fileupload
# temporary directory to write streams to
tempdir = %(here)s/data/fileupload

# file to inject in the html code
include_files = fileupload.css jquery.*

# if you already have jquery in your application, use this line
#include_files = fileupload.css jquery.fileupload.*

# max upload size is 50Mo
max_size = 50

# use this options to also wrap your application with a Storage middleware
#upload_to = %(here)s/storage
#exclude_paths = /@@Then you can access the javascript stuff at/gp.fileupload.static/.Theinclude_filesparameters will inject these tags in your application:<link type="text/css" rel="Stylesheet" media="screen"
      href="/gp.fileupload.static/fileupload.css"/>
<script type="text/javascript"
        src="/gp.fileupload.static/jquery.js"/>
<script type="text/javascript"
        src="/gp.fileupload.static/jquery.fileupload.js"/>
<script type="text/javascript"
        src="/gp.fileupload.static/jquery.fileupload.auto.js"/>And feel free to use ajax stuff. Notice that these tags are included at the end
of the html body.Available files are:jquery.js: jquery 1.2.6jquery.fileupload.js: thejQuery().fileUploadplugin.jquery.fileupload.auto.js: auto bind form tags with amultipart/form-dataenctype.fileupload.css: a few css to display the progress bar.Ajax stuffDescriptionAjQueryplugin is provided as a helper.To use it, you only need to add a script tag to your html head section:<script type="text/javascript">
    jQuery(document).ready(function() { jQuery('#sample').fileUpload(); });
</script>ThefileUploadplugin has the following options:replace_existing_form: replace the existing form with a generated one.Defaults to false.submit_label: The label of the general submit button.field_name: A string to use as file field name for each form. Default tofile.hidden_submit_name: A string to use in the hidden field name located beloweach file field. The hidden field is supposed to replace the missing button,
and is required by form frameworks (such as z3c.form) which expect a button
name (bound to an action). Default tosubmit.submit_empty_forms: if true, submit forms with empty file fields. Default:
true.use_iframes: If set tofalsethe form will be submitted as a normal form.
Iftruethe form target become an iframe and the page is not reloaded.stat_delay: delay between each stat request. Default: 1500.success: A javascript function evaluated when all files are uploaded. The
default one does nothing.ExamplesIf you want multiple file forms:<div id="forms"></div>

<script type="text/javascript">
  jQuery(document).ready(function() {
    jQuery('#forms').fileUpload({action:'/upload', field_name:'file_field'})
  });
</script>This will show a form with addable file field and upload them to/upload.
The forms are submitted in iframes as target so the page does not change after
uploading.If you already have forms. This is what is done in thejquery.fileupload.auto.js:<script type="text/javascript">
  jQuery(document).ready(function() {
    jQuery('form[enctype^='multipart/form-data]')
        .fileUpload({use_iframes: false});
  });
</script>This will show a progress bar when the form is uploaded, then redirect to the
application page when the upload is completed. So the usage is totally
transparent for you.ContributorsGael Pasgrimaud <gael@gawel.org>Christophe Combelles <ccomb@free.fr>News1.2 (2012-09-03)Use json instead of simplejson if you have Python2.6.Fix middleware to work on Windows.Add a flagrequire_sessionthat enable the middleware only if
there is SESSION_NAME in the query string. By default the behavior
is off, meaning it behave like before.1.1Unrecorded changes.1.0Unrecorded changes.0.9 (2010-03-20)support multiple file with same name and special chars (by pfalcon)allow JSON callback on stats (by pfalcon)0.8 (2009-02-20)fix installation problem0.7 (2009-02-08)fix #1. bug in js when the environ have a SCRIPT_NAMEstorage improvement. (by trollfot)0.6 (2008-11-24)improve middleware internals using WebOb0.5 (2008-08-28)support readline(int). Thanks to Tony Caduto for bug report.Added a hidden field to provide an action name
required by form frameworks such as z3c.formupdated the docraised the stat update delay to 1500msadded an option to replace the existing form with a generated one.changed the progress filename from a span to a divimproved usability by adding a form on input:file change eventhandle only POST methodimprove Storage middleware0.4 (2008-08-12)fix egg install scriptadd a storage module0.3 (2008-07-27)fix Content-Length header sent after code injection.adde.stopPropagation()to theAdd more filelink0.2 (2008-07-25)add docs/ folder to auto generate documentation with sphinxuse jquery packed versionIE javascript fixes0.1 (2008-07-24)first version