1

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.

1
  • I mean before uploading the file. Commented May 24, 2010 at 5:27

4 Answers 4

4

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

Sign up to request clarification or add additional context in comments.

3 Comments

@Flimm is kind of right kind of wrong. You can limit it client side, but that won't stop malicious users from uploading larger files.
It is possible to verify the upload client-side now. Of course, you still need to verify the size server-side, because the server cannot trust the client to be trustworthy.
@DustinWyatt I agree. I deleted my old comment.
3

You can do this in JavaScript in most recent browsers, using the File API: http://www.w3.org/TR/FileAPI/

For example (using jQuery):

var TYPES = ['image/jpeg', 'image/jpg', 'image.png'];

var file = $('#my_file_input')[0].files[0];
var size = file.size || file.fileSize;
var type = file.type;

if (size > MAX_BYTES) {
  alert('Error: file too large');

} else if (TYPES.indexOf(type) < 0) {
  alert('Error: file not a JPG or PNG');

} else {
  // proceed with file upload

}

No need for Java or Flash. Of course, you'll still need some sort of checking on the server, for users who disable JavaScript, or for malicious users. If you don't do server-side verification, you have a security vulnerability.

Comments

0

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.

1 Comment

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.
0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.