1.安装命令
npm i image-conversion --save
2.引入插件
import * as imageConversion from "image-conversion";
//
<div class="upload">
<van-uploader
accept="image/*"
v-model="imgList"
multiple
:max-count="5"
:max-size="8 * 1024 * 1024"
@oversize="onOversize"
/>
</div>
//methods
async handeImg(img) {
// 小于2M直接转成bolb对象 大于两m压缩成2000kb左右
let data = null;
if (img.size < 1 * 1024 * 1024) {
data = new Blob([img], { type: img.type });
} else {
data = await imageConversion.compressAccurately(img, 2000);
}
const base64 = await imageConversion.filetoDataURL(data);
return base64;
},
//监听 imgList上传数组
watch: {
async imgList(Nval) {
try {
this.certficationImage1 =
Nval[0] && (await this.handeImg(Nval[0].file));
this.certficationImage2 =
Nval[1] && (await this.handeImg(Nval[1].file));
this.certficationImage3 =
Nval[2] && (await this.handeImg(Nval[2].file));
this.certficationImage4 =
Nval[3] && (await this.handeImg(Nval[3].file));
this.certficationImage5 =
Nval[4] && (await this.handeImg(Nval[4].file));
} catch (error) {
console.log(error);
}
}
}