JavaScript file upload size validation

后端 未结 13 2157
眼角桃花
眼角桃花 2020-11-22 00:23

Is there any way to check file size before uploading it using JavaScript?

13条回答
  •  爱一瞬间的悲伤
    2020-11-22 01:21

    Works for Dynamic and Static File Element

    Javascript Only Solution

    function ValidateSize(file) {
            var FileSize = file.files[0].size / 1024 / 1024; // in MB
            if (FileSize > 2) {
                alert('File size exceeds 2 MB');
               // $(file).val(''); //for clearing with Jquery
            } else {
    
            }
        }
     

提交回复
热议问题