限定文件上传格式
实现代码
let that = this;
//限定文件格式
const filetypes = [".jpg", ".png", ".xls", ".pdf", ".xlsx"];
const filepath = event.target.value;
if (filepath) {
let isnext = false;
const fileend = filepath.substring(filepath.lastIndexOf("."));
if (filetypes && filetypes.length > 0) {
for (let i = 0; i < filetypes.length; i++) {
if (filetypes[i] == fileend) {
isnext = true;
break;
}
}
}
if (!isnext) {
const toastCfg = new ToastConfig(ToastType.ERROR, '', '不接受此文件类型!', 3000);
that.toastService.toast(toastCfg);
event.target.value = "";
that.fileName = '未选择任何文件';
return false;
}
} else {
return false;
}