views:

80

answers:

3

Hi How to restrict the size of file being uploaded. I am using django 1.1 with apache. Can I use apache for this and show some html error page if say size is bigger then 100MB.

Thanks.

A: 

apache has a server setting for max file size..(also dont forget max post size). I do not believe apache can show an error page on its own, you can probably use python for that. unfortunetly I know nothing obout python (yet) so I can't really help you beyond that. I know php can do that easily so I'm sure there is a method for python.

WalterJ89
ok I can use LimitRequestBody in apache for limiting the size.but can figure out to show error page if size is bigger from django.
laspal
+1  A: 

I mean before uploading the file

On client side it isn't possible...

I suggest to write a custom upload handlers and to override receive_data_chunk.

Example: QuotaUploadHandler

maersu
A: 

If you want to get the file size before uploading begins you will need to use Flash or a Java applet.

Writing a custom upload handler is the best approach. I think something like the following would work (untested). It terminates the upload as early as possible.

from django.conf import settings
from django.core.files.uploadhandler import FileUploadHandler, StopUpload

class MaxSizeUploadHandler(FileUploadHandler):
    """
    This test upload handler terminates the connection for 
    files bigger than settings.MAX_UPLOAD_SIZE
    """

    def __init__(self, request=None):
        super(MaxSizeUploadHandler, self).__init__(request)


    def handle_raw_input(self, input_data, META, content_length, boundary, encoding=None):
        if content_length > settings.MAX_UPLOAD_SIZE:
            raise StopUpload(connection_reset=True)
John Keyes