El-ui 解析图片为base64编码,并设置新增和编辑时图片预览

效果展示:

  新增时使用el-ui默认样式展示图片

 编辑时使用自己img样式展示图片:

    

el-ui选取图片代码:

<el-upload
       class="upload-demo"
       action=""
       :on-change="handleChange"
       :limit="1"
       :show-file-list="showImg"
       :auto-upload="false"
       :file-list="fileList"
       list-type="picture">
       <el-button size="small" type="primary">选取图片</el-button>
</el-upload>
 <ul v-if="!showImg" class="el-upload-list el-upload-list--picture">
     <li tabindex="0" class="el-upload-list__item is-ready">
          <img :src="imgUrl" alt class="el-upload-list__item-thumbnail">
     </li>
 </ul>

所需参数:

 fileList:[],
 showImg:true,
 imgUrl:''

所需方法:

 handleChange(file, fileList) {
        //是否使用el-ui默认图片样式
        this.showImg = true;
        var This = this;
        if(!this.volidImg(file)){
            return;
        }
       
        //获取base64
        var reader = new FileReader();
        reader.readAsDataURL(file.raw);
        reader.onload = function(e){ 
           
            This.imgUrl = this.result; //图片base64编码
        }
    },
    //校验是否为图片
    volidImg(file){
     
        const isJPG = file.raw.type === 'image/jpeg';
        const isPng = file.raw.type === 'image/png';
        const isLt2M = file.size / 1024 / 1024 < 2;
        if (!isJPG && !isPng) {
        this.$message.error('上传印章图片格式不符合!');
        this.fileList = [];
        }
        if (!isLt2M) {
        this.$message.error('上传印章图片大小不能超过 2MB!');
         this.fileList = [];
        }
        return (isJPG || isPng) && isLt2M;

    }

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值