上传图片-限制尺寸大小

1.图片upload之前做监听,>-- before-upload

<div class="demo-upload-list" v-for="i in uploadImgList">
        <template>
            <img :src="i">
            <div class="demo-upload-list-cover">
                <Icon type="ios-trash-outline" @click.native="handleRemove()"></Icon>
            </div>
        </template>
        <template v-else>
            <Progress v-if="item.showProgress" :percent="item.percentage" hide-info></Progress>
        </template>
      </div>
      <Upload
        v-if="uploadImgList.length == 0"
        ref="upload"
        :show-upload-list="false"
        :format="['jpg','jpeg','png']"
        :before-upload="handleBeforeUpload"
        type="drag"
        action="#"
        style="display: inline-block;width:58px;">
        <div style="width: 58px;height:58px;line-height: 58px;" class="upload-icon">
            <Icon type="ios-camera" size="20" ></Icon>
        </div>
      </Upload>

handleBeforeUpload:上传文件之前的钩子,参数为上传的文件,若返回 false 或者返回 Promise 且被 reject,则停止上传。
这里会用到Promise ,及同步异步问题。

      async xzhandleUpload(file) {
          // 有大小限制
            var isImage = false;
            await this.checkImageWH(file,this.whSize).then(params => {
                isImage = params;
            });
            if (!isImage) {
                return;
            } 
            // 图片验证通过,下一步请求上传图片
            this.handleUpload(file);
        },

checkImageWH--在这里就是进一步通过获取image的大小,判断上传的图片是否符合规定。

    async checkImageWH(file) {
            return new Promise((resolve, reject)=> {
                let _this = this;
                let filereader = new FileReader();
                filereader.onload = (e) => {
                    let src = e.target.result;
                    const image = new Image();
                    image.onload = (e) => {
                       // w 、h设置的限制参数
                        if (image.width == w && image.height == h) {
                            resolve(true);
                        } else {
                            _this.$Notice.warning({
                                title: '图片尺寸为'+ this.whSize
                            })
                            reject(false);
                        }
                    };
                    image.onerror = reject;
                    image.src = src;
                };
                filereader.readAsDataURL(file);
            }).catch((e) => {});
        },


 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

季夏梧桐

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值