js前端驗證上傳的文件大小及格式


js前端驗證上傳的文件大小及格式
<script type="text/javascript"> function fileChange(target) { var fileSize = 0; if (!target.files) { var filePath = target.value; var fileSystem = new ActiveXObject("Scripting.FileSystemObject"); var file = fileSystem.GetFile(filePath); fileSize = file.Size; } else { fileSize = target.files[0].size; } var size = fileSize / 1024; if (size <110) { //空白的Excel表最大為10.1KB,當小於它及判斷為Excel內容為空 alert("導入的Excel表不能為空!"); target.value = ""; return } if (size > 5000) { alert("導入的Excel表不能大於5M!"); target.value = ""; return } var name = target.value; var fileName = name.substring(name.lastIndexOf(".") + 1).toLowerCase(); if (fileName != "xlsx" && fileName != "xls") { alert("請選擇.xlsx格式文件上傳!"); target.value = ""; return } } </script>

 

調用JS的input框

 <td><input type="file" id="file" name="file" accept="application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" onchange="fileChange(this);"></td>

 

原文鏈接:https://www.jianshu.com/p/85059c9752d8


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM