How to restrict the size of file being uploaded apache + django

后端 未结 4 1939
北荒
北荒 2021-01-17 00:49

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.

4条回答
  •  再見小時候
    2021-01-17 01:09

    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)
    

提交回复
热议问题