我想将一个事件附加到我网站上的所有输入类型="file",以检查文件大小是否大于 5mo,如果是,则阻止上传并显示警告。可能吗?
最佳答案
您可以使用 jQuery 绑定(bind)更改事件:
$('input[type="file"]').change(function(){
var f=this.files[0];
var sizeInMb = f.size/1024;
var sizeLimit= 1024*5; // if you want 5 MB
if (sizeInMb > sizeLimit) {
alert('Sorry the file exceeds the maximum size of 5 MB!');
// reset the input (code for all browser)
this.replaceWith(input.val('').clone(true));
return false;
}
else {
// go on
}
}
希望这有帮助。
关于javascript - 如何在所有输入类型 ="file"上传之前附加一个事件来检查文件大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30117168/