Vue-【图片上传组件】

<template>
  <div class="upload">
    <div class="uploadFile">
      <div class="previewBox">
        <div class="uploadPreview" v-if="uploadPicPreview">
          <ms-viewer :imgUrl="'data:image/jpg;base64,' + uploadPicPreview"></ms-viewer>
        </div>
        <img :src="preview" v-else>
      </div>
      <div class="uploadIcon" v-show="uploadIcon">
        <input type="file" id="fileName" ref="inputFile" :disabled="disabled" @change="fileImage" :accept="supportType">
        <label for="fileName">
          <img src="@/base/assets/realName/icon.png">
        </label>
      </div>
    </div>
    <div class="examplePic" v-show="exampleShow">
      <ms-viewer :imgUrl="example" v-if="example"></ms-viewer>
      <ms-viewer :imgUrl="png1" v-else></ms-viewer>
    </div>
 </div>
</template>
<script>
import png1 from '@/base/assets/realName/bankAccountPermit.png'
import png2 from '@/base/assets/realName/front.png'
export default {
  name: 'ms-upload',
  data () {
    return {
      imgURL: '',
      png1
    }
  },
  methods: {
    fileImage (e) {
      let $this = this
      let img = e.target.files[0]
      if (img) {
        let fileSize = img.size
        let size = fileSize / 1024
        // 限制上传文件的大小
        if (size > (this.fileSizeNumber * 1000)) {
          this.$Confirm.open({
            title: '消息',
            content: '上传图片不能超过' + this.fileSizeNumber + 'M,注意压缩图片',
            icon: 2,
            confirm: {
              text: '确认',
              show: true,
              callback: () => {
                this.$Confirm.close()
              }
            },
            cancel: {
              text: '',
              show: false
            }
          })
        } else {
          if (window.FileReader) {
            let reader = new FileReader() // 创建一个reader
            reader.readAsDataURL(img) // 将图片将转成 base64 格式
            reader.onloadend = function () {
              let suffixData = ''
              if (e.target.files[0].name) {
                suffixData = e.target.files[0].name
                  .substr(e.target.files[0].name.lastIndexOf('.') + 1).toLowerCase()
                if ($this.supportType.indexOf(suffixData) == '-1') {
                  $this.$Global.messageAlert($this, `请上传${$this.supportType}文件`)
                  return
                }
              }
              $this.imgURL = reader.result
              $this.$emit('on-change', reader.result.split(',')[1], suffixData)
            }
          } else {
            this.$Global.messageAlert(this, '您的浏览器不支持图片上传,请升级您的浏览器')
          }
        }
      }
    }
  },
  props: {
    disabled: {
      type: Boolean,
      default: false
    },
    fileSizeNumber: {
      type: Number,
      default: 5
    },
    supportType: {
      type: String,
      default: 'image/gif, image/jpg, image/jpeg, image/png'
    },
    preview: {
      type: String,
      default: png2
    },
    example: {
      type: String,
      default: ''
    },
    exampleShow: {
      type: Boolean,
      default: true
    },
    uploadPicPreview: {
      type: String,
      default: ''
    },
    fileName: {
      type: String,
      default: 'file'
    },
    uploadIcon: {
      type: Boolean,
      default: true
    }
  },
  watch: {
    imgURL: {
      handler (val, oldVal) {
        // console.log(val + 'hrhrh' + oldVal)
        // console.log(this.imgURL)
      },
      deep: true
    },
    uploadPicPreview: {
      handler (val, oldVal) {
        // console.log(val + 'hrhrh' + oldVal)
        // console.log(this.uploadPicPreview)
      },
      deep: true
    }
  }
}
</script>
<style lang="scss">
  @import '@/lib/scss/_var.scss';
  @import '@/src/scss/_main.scss';
  .upload {
    .uploadFile,.examplePic {
      display: inline-block;
      width: px2em(180px);
      height: px2em(120px);
      img {
        width: 100%;
        height: 100%;
      }
    }
    .uploadFile {
      position: relative;
      .previewBox {
        width: 100%;
        height: 100%;
      }
      .uploadIcon {
        position: absolute;
        right: px2em(15px);
        bottom: px2em(15px);
        width: px2em(60px);
        height: px2em(60px);
        input {
          display: none;
        }
        label {
          display: inline-block;
          width: px2em(60px);
          height: px2em(60px);
          cursor: pointer;
        }
      }
    }
    .examplePic {
      position: relative;
      left: px2em(30px);
    }
  }
</style>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值