Re: [cherrypy-users] Large file upload with progress

455 views
Skip to first unread message

V G

unread,
Sep 17, 2012, 2:23:14 AM9/17/12
to cherryp...@googlegroups.com
On Wed, Sep 12, 2012 at 6:08 PM, veegee <x.sola...@gmail.com> wrote:
Hi all,

I have searched all over and was unable to find a way to provide upload progress for file uploads. The closest thing I could find was this: http://docs.cherrypy.org/stable/progguide/streaming.html but I don't know how much that has to do with uploads (as opposed to responses), and this http://tools.cherrypy.org/wiki/DirectToDiskFileUpload but I don't know if it still applies to CherryPy 3.2.2.

Is there any way to handle uploads directly and get upload progress?

I'm using CherryPy 3.2.2 and Python 3.2.3

Does anyone have any suggestions at all?

Joel Rivera

unread,
Sep 17, 2012, 3:46:07 AM9/17/12
to cherryp...@googlegroups.com
You can use the idea of the fileUpload tool and then in the same tool
add to the cherrypy.session the temporary file and external id (which
can be created with javscript and should be unique from the client side)
and the size of the request:

e.g.

POST /filemgm/_upload?fid=1234ab

def bigfile(self, fid, **kwargs):
...
...
cherrypy.session[fid] = \
(TEMPORARY_FILEPATH,
cherrypy.request.headers['Content-length'])
cherrypy.session.save()
...
...

Then periodically interrogate the server asking for the file

GET /filemgm/_status?fid=1234ab

the filemgm._status method adds the logics to return the upload
percentage which you can know from the session, the fid parameter and
the percentage from the relation of the header 'Content-length' and the
current size of the file.

e.g.

import os

def _status(self, fid):
try:
tempfilepath, length = cherrypy.session[fname]
except (TypeError, ValueError): # is None or unpack error
return 'done'
else:
currsize = os.stat(tempfilepath).st_size
return float(currsize) / length * 100 # float is just for python2


For this to work you need to have some client-side code to keep updating
the percentage to the user, you can easily make and async post with
jquery to post the first request and then ask for the status of the fid.

The fundamental problem is that you cannot respond to the client
when the uploading of the request (the file) is being done, so I think
that the solution should be in the form of a secondary request.

The essence from the idea that I propose is
1.- Had an external id of the file that you are uploading.
2.- Make a secondary request to check the status of the id that the
first request make, which can be one with an streamed response
or many with precise responses.

Also I'm overlooking that when the request is getting processed in the
first request the file that has being created gets periodically flushed
to have a trustworthy uploading percentage.

I hope that help you to consider this as an alternative.

Cheers.
> --
> You received this message because you are subscribed to the Google
> Groups "cherrypy-users" group.
> To post to this group, send email to cherryp...@googlegroups.com.
> To unsubscribe from this group, send email to cherrypy-users
> +unsub...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/cherrypy-users?hl=en.

--
Rivera²

V G

unread,
Sep 17, 2012, 4:11:20 PM9/17/12
to cherryp...@googlegroups.com
Excellent! That was exactly what I was looking for. Thanks so much!

Tim

unread,
Nov 23, 2012, 7:44:54 AM11/23/12
to cherryp...@googlegroups.com
How do i find the temporary file does the temporary file get deleted as soon as the upload handler returns?
Reply all
Reply to author
Forward
0 new messages