iview上传图片控制格式、文件大小、宽高

<template>
 <Upload id="Upload"
          ref="upload"
         :before-upload='beforeUpload'
         type="drag"
         action='#'
         style="display: inline-block;width:97px;">
          <div style="width: 97px;height:90px;line-height: 90px;"
               v-show="imageUrl == false">
                <Icon type="md-camera"
                      size="20"></Icon>
          </div>
          <div style="width: 97px;height:90px;line-height: 90px;"
               v-show="imageUrl == true">
               <img style='width:100%;height:100%'
                    :src="seriesImage"
                     alt="">
           </div>
  </Upload>
</template>

<script>
import gifLoading from ../assets/icon/loading.gif'
export default {
 data(){
    imageUrl: false,
    seriesImage: gifLoading,
    imgSize: 800,                               // 上传图片大小限制,单位 kb
    imgTypeArr: ["image/png", "image/jpg", "image/jpeg"],//上传图片类型限制
 },
 methods:{
    //上传文件之前钩子函数
    beforeUpload (res) {
      let that = this
      // 控制文件上传格式
      var imgType = this.imgTypeArr.indexOf(res.type) !== -1
      if (!imgType) {
        this.$Message.warning({
          content: '文件  ' + res.name + '  格式不正确, 请选择jpg或png.',
          duration: 5
        });
        return false
      }
      // 控制文件上传大小
      var Maxsize = res.size / 1024 < this.imgSize
      if (!Maxsize) {
        this.$Message.warning({
          content: '文件  ' + res.name + ' 体积过大, 最大不能超过800K.',
          duration: 5
        });
        return false
      }
      this.file = res
      this.checkImageWH(res, 750, 422)
      return false
    },
    //判断照片尺寸大小
    checkImageWH (file, width, height) {
      let self = this;
      return new Promise(function (resolve, reject) {
        let filereader = new FileReader();
        filereader.onload = e => {
          let src = e.target.result;
          var status = 0
          const image = new Image();
          image.onload = () => {
            if (width && image.width > width) {
              self.$Message.error(
                '高度不得大于' + width
              );
              status = -1
              reject();
            } else if (height && image.height > height) {
              self.$Message.error(
                '宽度不得大于' + height
              );
              status = -1
              reject();
            } else {
              resolve();
            }
            if (status == 0) {
              // 图片base64化   可以在这里设置路径预览图片
              self.DetailsObj.seriesImage = src;
            }
          };
          image.onerror = reject;
          image.src = src;
        };
        filereader.readAsDataURL(file);
      });
    },
 }
}
</script>

<style lang="scss" scoped>

</style>
  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值