function getImgByteSize(data:string) {
if (data) {
const equalIndex = data.indexOf('='); // 获取=号下标
// 根据不同图片格式切割,jpg,jpeg,png,gif
const isBase = data.substr(15,4)
if (isBase === 'base') {
data = data.substring(22)
} else if (isBase === ';bas'){
data = data.substring(23)
}
if (equalIndex > 0) {
const str = data.substring(0, equalIndex); // 去除=号
const strLength = str.length;
const fileLength = strLength - (strLength / 8) * 2; // 真实的图片byte大小
console.log('字节',fileLength)
var size = '';
size = (fileLength / 1024).toFixed(2);
var index = size.indexOf("."); //获取小数点处的索引
var dou = size.substr(index + 1, 2) //获取小数点后两位的值
if (dou == "00") { //判断后两位是否为00,如果是则删除00
return Number(size.substring(0, index))
}
return Number(size);
} else {
const strLength = data.length;
const fileLength = strLength - (strLength / 8) * 2;
console.log('字节',fileLength)
var size = '';
size = (fileLength / 1024).toFixed(2);
var index = size.indexOf("."); //获取小数点处的索引
var dou = size.substr(index + 1, 2) //获取小数点后两位的值
if (dou == "00") { //判断后两位是否为00,如果是则删除00
return Number(size.substring(0, index))
}
return Number(size);
}
} else {
return null;
}
}
base64图片转换kb
最新推荐文章于 2022-08-18 15:30:33 发布