我有这个代码来防止上传大文件(使用JQuery-1.8.3。我更喜欢保留这个版本):
<script type="application/javascript">
$(function() {
var form = $("#add-attachment");
if(form.length !== 0)
{
var submitted = false;
form.on('submit', function (e) {
var file = $("input[type=file]")[0];
if(submitted == false) {
e.preventDefault();
var fileOk;
if (typeof file.files === "undefined") fileOk = file;
else fileOk = file.files[0];
if(fileOk.size > 2000000) {
alert("File too large");
$("#add-attachment")[0].reset();
} else {
submitted = true;
$(this).submit();
}
}
});
}
});
</script>
<form id="add-attachment" method="post" enctype="multipart/form-data">
<div id="form">
<div>
<label for="form_file" class="required"></label>
<input type="file" id="form_file" name="form[file]" required="required">
</div>
<input type="hidden" id="form__token" name="form[_token]" value="4353454554544554545">
</div>
<p><small>Tamaño máximo: 2 MB</small></p>
<input type="submit" class="btn" value="Adjuntar">
</form>
当文件超过2000000B时,这只是执行alert
。
但它在IE8上没有做任何事情。知道为什么吗?