1. 可以在input上傳組件上添加屬性accept,這樣上傳文件的時候,就只能選擇excel文件了。
<input type="file" accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel" />
2.通過正則判斷
if (excel!= '') {
var reg = /^.*\.(?:xls|xlsx)$/i;//文件名可以帶空格
if (!reg.test(path)) {//校驗不通過
alert("請上傳excel格式的文件!");
return;
}
}