How to check file input size with jQuery?

前端 未结 8 2413
渐次进展
渐次进展 2020-11-22 09:21

I have a form with file upload capabilities and I would like to be able to have some nice client side error reporting if the file the user is trying to upload is too big, is

8条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-22 09:54

    I am posting my solution too, used for an ASP.NET FileUpload control. Perhaps someone will find it useful.

        $(function () {        
        $('<%= fileUploadCV.ClientID %>').change(function () {
    
            //because this is single file upload I use only first index
            var f = this.files[0]
    
            //here I CHECK if the FILE SIZE is bigger than 8 MB (numbers below are in bytes)
            if (f.size > 8388608 || f.fileSize > 8388608)
            {
               //show an alert to the user
               alert("Allowed file size exceeded. (Max. 8 MB)")
    
               //reset file upload control
               this.value = null;
            }
        })
    });
    

提交回复
热议问题