Skip to content Skip to sidebar Skip to footer

How To Restrict Image Upload Size To Less Than 2mb?

I have an html select option for uploading images.
Attach Image

Solution 1:

This example should give you an idea of how to do it:

HTML

<formclass="upload-form"><inputclass="upload-file"data-max-size="2048"type="file" ><inputtype=submit></form>

JS

$(function(){
    var fileInput = $('.upload-file');
    var maxSize = fileInput.data('max-size');
    $('.upload-form').submit(function(e){
        if(fileInput.get(0).files.length){
            var fileSize = fileInput.get(0).files[0].size; // in bytesif(fileSize>maxSize){
                alert('file size is more than ' + maxSize + ' bytes');
                returnfalse;
            }else{
                alert('file size is correct - '+fileSize+' bytes');
            }
        }else{
            alert('Please select the file to upload');
            returnfalse;
        }

    });
});

Post a Comment for "How To Restrict Image Upload Size To Less Than 2mb?"