思路
判断 file.type
应该是最简单直接的方式了,但是这种方法用在图片上传时非常管用,但是。。。excel 文件的 file.type
。。。看图吧。。。。


额。。这。。。。
算了,干脆截取文件名的后缀名来判断吧
限制excel文件格式
beforeUpload = file => {
const fileTypes = ['xlsx', 'xls']; // 文件格式
const thisType = file.name.slice(file.name.lastIndexOf('.') + 1); // 从 file.name 中截取后缀名
const isExcel = fileTypes.includes(thisType);
if (!isExcel) {
message.error('只能上传excel文件!');
}
return isExcel;
}
限制图片上传,可直接判断 file.type
beforeUpload = file => {
const picType = ['image/jpeg', 'image/png']; // 图片格式
const isPic = !(picType.indexOf(file.type) === -1); // 判断是否是pic的类型
if (!isPic) {
message.error('请上传jpg或者png格式的图片');
}
const isLt4M = file.size / 1024 / 1024 < 4;
if (!isLt4M) {
message.error('图片大小不能超过4M');
}
return isPic && isLt4M;
};
网友评论