views:

904

answers:

4

I know that when we upload to a web application which use django, we couldn't access the upload file before it completely receive by the server.

So my question, is there any way to check/access the total byte of upload file in progress?

thanks

+2  A: 

You can monitor an upload in progress by writing your own Django upload handler. All you need to do is subclass django.core.files.uploadhandler.FileUploadHandler and implement the receive_data_chunk and file_complete methods.

Django's upload handler documentation gives you the details you need.

Jarret Hardie
well, the handler won't signal anything before all the file is uploaded to memory/file. so my question is how we track bytes of file which is uploaded to memory/temporary file
The receive_data_chunk method in your custom file upload handler receives the data as it arrives... it doesn't wait until the entire file has been uploaded.
Jarret Hardie
+1  A: 

It depends on your server setup. If you use Apache with mod_python then files are streamed to your Django process as they arrive. But if you run Django behind a web server using wsgi or fastcgi, then the whole file will be buffered by the server before django gets involved. Then the solution given above wont work.

This is "by design" because tying up fastcgi processes waiting for file uploads would be a big waste. So what you have to do instead is use a server module such as http://redmine.lighttpd.net/wiki/1/Docs%3AModUploadProgress for Lighttpd, which implements the upload_progress view for you on the server. Or http://wiki.nginx.org/NginxHttpUploadProgressModule for nginx.

Björn Lindqvist
Your generalisation about WSGI is wrong. WSGI is an interface definition and there are many implementations of it and how they behave can be different and with such a matter as how streaming of request content is handled more often to do with the web server they are used in conjunction with and not the WSGI implementation. For example, in Apache/mod_wsgi itself there is no buffering of request content and it can be streamed. You stick a nginx front end proxy in front of Apache/mod_wsgi though and that changes though as nginx buffers when proxying.
Graham Dumpleton
A: 

Just found an upload progress module for apache:

http://github.com/drogus/apache-upload-progress-module

What I don't know is how (or whether) it's works with django & mod_wsgi.

Till Backhaus