思路
判斷 file.type
應該是最簡單直接的方式了,但是這種方法用在圖片上傳時非常管用苫幢,但是访诱。。韩肝。excel 文件的 file.type
触菜。。哀峻∥邢啵看圖吧哲泊。。漾峡。攻旦。
xlsx后綴的excel
xls后綴的excel
額喻旷。生逸。這。且预。槽袄。。
算了锋谐,干脆截取文件名的后綴名來判斷吧
限制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;
};