gp.fileuploadΒΆ

Author:unknown
Version:unknown

NOTE: Incomplete. Maybe needs zip with full example code.

Using a middleware for larger file uploads is advisable. Very long uploads will block server threads and create long running transactions. This is something you usually want to avoid.

You also might want to show a progress bar to your users. For this you need to be able to query the current upload progress.

gp.fileupload does exactly this. It integrates into the WSGI stack. Basically, when a user uploads a file the gp.fileupload stage of the pipeline will process the incoming data and provide a way to query progress. Once the upload is complete your application will receive a POST request with the filename of the file stored in a temporary folder. You can then use it for your own purposes.

Just add gp.fileupload to your setup.py. Then add a snippet like below to your debug.ini/deploy.ini .

Snippet from debug.ini:

[filter:fileupload]
use = egg:gp.fileupload
tempdir= %(here)s/tmp/
upload_to = %(here)s/myapp.upload.files/
include_files = jquery.fileupload.*
exclude_paths = /@@ /.+\.direct
max_size = 500

[pipeline:main]
pipeline = fileupload egg:myapp#debug

This puts the fileupload module in the WSGI stack.

From your grok.View class serve html code 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 id must be 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/1

This will return some JSON data like:

{'state': 1, 'percent': 69}

state can 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.

For detailed information please take a look at these two pages:

_gp.fileupload documentation: http://www.gawel.org/docs/gp.fileupload/ and _PyPI page of gp.fileupload: http://pypi.python.org/pypi/gp.fileupload .