vue项目中,上传图片做像素大小宽高的限制

vue项目中,上传图片做像素大小宽高的限制

<el-upload
  class="avatar-uploader"
       action="http://upload.qiniup.com"
       :data="qn"
       :show-file-list="false"
       :on-success="handleAvatarSuccess"
       :before-upload="beforeAvatarUpload">
   <img v-if="form.joint.imgUrlBefore" :src="form.joint.imgUrlBefore"
        class="avatar" @click="getToken('imgUrlBefore','poster_url')">
   <i v-else class="el-icon-plus avatar-uploader-icon"
      @click="getToken('imgUrlBefore','poster_url')"></i>
</el-upload>
  /**
	*  上传前判断
	*/
	beforeAvatarUpload(file) {
	  
	   const isJPG = file.type === 'image/jpeg' || file.type === 'image/jpg' || file.type === 'image/png';
	   const isLt1M = file.size / 1024 / 1024 < 1;
	
	   if (!isJPG) {
	       this.$message.error('上传头像图片只能是 JPG/PNG 格式!');
	   }
	   if (!isLt1M) {
	       this.$message.error('上传头像图片大小不能超过 1MB!');
	   }
	
	   let _this = this;
	   let imgWidth = "";
	   let imgHeight = "";
	   let width;
	   let height;
	   const isSize = new Promise(function (resolve, reject) {
	       if (_this.form.ad_type == 1) {
	           width = 700;
	           height = 300;
	       } else if (_this.form.ad_type == 2) {
	           width = 800;
	           height = 1280;
	       }
	       console.log(width, height);
	       let _URL = window.URL || window.webkitURL;
	       let img = new Image();
	       img.onload = function () {
	           imgWidth = img.width;
	           imgHeight = img.height;
	           let valid = img.width == width && img.height == height;
	           valid ? resolve() : reject();
	       };
	       img.src = _URL.createObjectURL(file);
	   }).then(
	       () => {
	           return file;
	       },
	       () => {
	           _this.$message.error({
	               message: '上传文件的图片大小不合符标准,宽需要为' + width + 'px,高需要为' + height + 'px。当前上传图片的宽高分别为:' + imgWidth + 'px和' + imgHeight + 'px',
	               btn: false
	           });
	           return Promise.reject();
	       });
	   console.log(isSize);
	   return isJPG && isLt1M && isSize;
	},
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值