2010-05-24 57 views

回答

0

apache有一个最大文件大小的服务器设置..(也不要忘记最大发布大小)。我不相信apache可以自己显示一个错误页面,你可以使用python。 不幸的是,我不知道什么obout python(尚未),所以我不能真正帮助你。 我知道PHP可以做到这一点,所以我相信有一种方法为Python。

+1

好的我可以在apache中使用LimitRequestBody来限制大小。 ,但如果尺寸大于django,可以显示出错误页面。 – laspal 2010-05-24 07:01:52

0

如果你想获得文件大小上传开始前,你需要使用Flash或Java小程序。

编写自定义上传处理程序是最好的方法。我认为像下面这样的东西可以工作(未经测试)。它会尽早终止上传。

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) 
1

您可以在大多数最新的浏览器做到这一点在JavaScript中,使用文件API:http://www.w3.org/TR/FileAPI/

例如(使用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 

} 

无需为Java或Flash。当然,对于禁用JavaScript的用户,您仍然需要在服务器上进行某种检查。

相关问题