记录一下图片压缩并上传的过程。
1、使用ElementUI的上传组件,这里用http-request覆盖了默认的上传行为
<el-upload
:show-file-list="false"
:http-request="uploadIdCard"
action=""
class="avatar-uploader">
<img v-if="frontPhoto" class="idCard" :src="frontPhoto" alt="" />
<img v-if="!frontPhoto" class="idCard" src="@/assets/images/yjj_idCard.png" alt />
<img v-if="!frontPhoto" class="add" src="@/assets/images/yjj_add.png" alt />
</el-upload>
2、业务需求是上传的图片要小于2MB,这里先对图片大小做判断,并将图片格式转为base64便于下一步的压缩
// 判断图片是否需要压缩
imgPreview (file) {
console.log(file)
let _this = this
// 看是否支持fileReader
if (!window.FileReader) {
Toast({
message: '您的设备不支持FileReader',
duration: 2000
})
return
}