Hi
Could you please provide some examples, script with details level steps ?
My requirement is, I am building an app where user can upload image using ckeditor but the problem is right now, I can upload any size of image. Basically i need to upload only less than 2MB file pics. So how to pop up an alert and ask users to upload 2MB file , until that it should not upload into server.
Please provide more details on the same.
Thank you. But this one is RichTextEditor field. How to read the image file alone from this field in Django?
- bhavanaswvarn 4 years, 6 months ago
@Bhavanswvarn
I found this interesting article Django File Size. We can limit the size of the image using our MAX_UPLOAD_SIZE
def clean_content(self):
content = self.cleaned_data['content']
content_type = content.content_type.split('/')[0]
if content_type in settings.CONTENT_TYPES:
if content._size > settings.MAX_UPLOAD_SIZE:
raise forms.ValidationError(_('Please keep filesize under %s. Current filesize %s') % (filesizeformat(settings.MAX_UPLOAD_SIZE), filesizeformat(content._size)))
else:
raise forms.ValidationError(_('File type is not supported'))
return content
Check this if it helps. If not, I will not try to find other alternate options.
In settings file you need to add
MAX_UPLOAD_SIZE = "preferred file size"
from django.conf import settings
def check_image_field_size(self):
content = self.cleaned_data.get('image_field')
if content._size > settings.MAX_UPLOAD_SIZE:
raise forms.ValidationError(_('Please keep filesize under %s. Current filesize %s') % (filesizeformat(settings.MAX_UPLOAD_SIZE), filesizeformat(content._size)))
return content
Use fseek to know the size of the file in bytes and check the condition