JavaScript文件上载大小验证

JavaScript文件上载大小验证

有没有办法检查文件大小在用JavaScript上传之前?



交互式爱情
浏览 402回答 3
3回答

狐的传说

使用jQuery:<form&nbsp;action="upload"&nbsp;enctype="multipart/form-data"&nbsp;method="post"> &nbsp;&nbsp;&nbsp;&nbsp;Upload&nbsp;image:&nbsp;&nbsp;&nbsp;&nbsp;<input&nbsp;id="image-file"&nbsp;type="file"&nbsp;name="file"&nbsp;/> &nbsp;&nbsp;&nbsp;&nbsp;<input&nbsp;type="submit"&nbsp;value="Upload"&nbsp;/> &nbsp;&nbsp;&nbsp;&nbsp;<script&nbsp;type="text/javascript"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$('#image-file').bind('change',&nbsp;function()&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;alert('This&nbsp;file&nbsp;size&nbsp;is:&nbsp;'&nbsp;+&nbsp;this.files[0].size/1024/1024&nbsp;+&nbsp;"MB"); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}); &nbsp;&nbsp;&nbsp;&nbsp;</script></form>

BIG阳

适用于动态和静态文件元素JavaScript唯一解function ValidateSize(file) {&nbsp; &nbsp; &nbsp; &nbsp; var FileSize = file.files[0].size / 1024 / 1024; // in MB&nbsp; &nbsp; &nbsp; &nbsp; if (FileSize > 2) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; alert('File size exceeds 2 MB');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// $(file).val(''); //for clearing with Jquery&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp;<input onchange="ValidateSize(this)" type="file">
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript