JavaScript file upload size validation

后端 未结 13 2150
眼角桃花
眼角桃花 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 {
    
            }
        }
     <input onchange="ValidateSize(this)" type="file">

    0 讨论(0)
提交回复
热议问题