As I Solve

Bulletproof solutions for the savvy developer.

Disallow File Uploads over 2Mb (or any size)


A quick and easy jQuery snippet to find all file upload inputs & make sure users don’t upload files that are too large.

Just include in your $(function(){ }); function

    $('input[type=file]').each(function(){
        $(this).on('change', function() {
            //this.files[0].size gets the size of your file.
            if (this.files[0].size > 2097152 || this.files[0].filesize > 2097152) {
                alert('Your file is too large (over 2Mb). Please use a smaller file.')
                $(this).wrap('<form>').closest('form').get(0).reset();
                $(this).unwrap();
            }
        })
    });

,

Leave a Reply