1👍
✅
You can use something like this:
CONTENT_TYPES = ['image']
MAX_UPLOAD_PHOTO_SIZE = "2621440"
content = request.FILES.getlist("file")
content_type = content.content_type.split('/')[0]
if content_type in CONTENT_TYPES:
if content._size > MAX_UPLOAD_PHOTO_SIZE:
#raise size error
if not content.name.endswith('.jpg'):
#raise jot jpg error
else:
#raise content type error
UPDATED
If you want a form
validation, try this:
class FileUploadForm(forms.Form):
file = forms.FileField()
def clean_file(self):
CONTENT_TYPES = ['image']
MAX_UPLOAD_PHOTO_SIZE = "2621440"
content = self.cleaned_data['file']
content_type = content.content_type.split('/')[0]
if content_type in CONTENT_TYPES:
if content._size > MAX_UPLOAD_PHOTO_SIZE:
msg = 'Keep your file size under %s. actual size %s'\
% (filesizeformat(settings.MAX_UPLOAD_PHOTO_SIZE), filesizeformat(content._size))
raise forms.ValidationError(msg)
if not content.name.endswith('.jpg'):
msg = 'Your file is not jpg'
raise forms.ValidationError(msg)
else:
raise forms.ValidationError('File not supported')
return content
👤levi
Source:stackexchange.com