防止使用Javascript(JQuery)更改文件输入
问题描述:
如果我有条件地告诉用户文件太大,我试图防止文件输入被更改,但是即使捕获更改并防止默认设置,它仍然会被填充,即:
I'm trying to prevent a file input being changed if I conditionally tell the user the file is too large, but even capturing the change and preventing default, it's still filled in ie:
// CHECK FILE ISNT ABOVE 500MB ON CHANGE
$(".upload-file").change(function (e) {
// conditional checks
var fileSize = $('.upload-file').get(0).files[0].size; // in bytes
if (fileSize > 500000000) {
e.preventDefault();
swal('File size is more than 500 MB, please upload a smaller file. Support for larger files will be rolled out soon.');
return false;
}
});
这是我的代码,我在做什么错?
Here's my code, what am I doing wrong?
答
如果要重置文件输入:
$(".upload-file").val(null);