1. 程式人生 > >js jquery 上傳文件格式大小判斷簡單總結

js jquery 上傳文件格式大小判斷簡單總結

大小 .text 錯誤 script console class hint pat asc

大家好我是床前明月光,疑是地上霜

// html

<a href="javascript:;" class="file">添加附件
<input type="file" name="" id="fileId" onchange="upload()">
</a>
/**判斷上傳文件文件格式**/
var filePath ="";
var fileType ="";
function upload(){
var fileName = $(‘#fileId‘).val().split(‘\\‘); //得到文件名數組
var fileSize = document.getElementById(‘fileId‘).files[0]; //獲得文件大小;
fileName2 = fileName[fileName.length-1]; // 獲得文件名
filePath = $(‘#fileId‘).val().toLowerCase().split(".");
fileType = filePath[filePath.length - 1]; //獲得文件結尾的類型如 zip rar 這種寫法確保是最後的
$(‘.errHint‘).show().text(fileName[2]);
console.log(filePath);
console.log(fileType);
if(!(fileType == "png" || fileType == "jpg" || fileType == "xlsx"|| fileType == "docx"|| fileType == "rar" || fileType == "zip" )){
$(‘.errHint‘).show().text(‘文件格式不符合要求!‘)
}else if(fileSize.size>10485760){
$(‘.errHint‘).show().text(‘錯誤!請上傳不超過10M的文件‘);
return
}
}
拷貝直接用即可

js jquery 上傳文件格式大小判斷簡單總結