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

后端 未结 4 1989
北荒
北荒 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:19

    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.

提交回复
热议问题