How to handle the image upload in Django ckeditor?

Submitted 3 years, 6 months ago
Ticket #227
Views 628
Language/Framework Django
Priority Urgent
Status Closed

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.

Submitted on Oct 10, 20

Can you please let us know the status ?. If everything looks then kindly close the ticket. - Vengat 3 years, 6 months ago

Second follow up on your ticket, is your issue has been resolved? - Vengat 3 years, 6 months ago
add a comment

3 Answers

Use fseek to know the size of the file in bytes and check the condition

Submitted 3 years, 6 months ago

Thank you. But this one is RichTextEditor field. How to read the image file alone from this field in Django?

- bhavanaswvarn 3 years, 6 months ago


Verified

@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.

Submitted 3 years, 6 months ago

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

Submitted 3 years, 6 months ago


Latest Blogs