Blame view

src/components/publicPicture/index.vue 2.13 KB
任超 committed
1
<template>
任超 committed
2
  <el-image-viewer :on-close="closeViewer" :url-list="urlList">
任超 committed
3 4 5
  </el-image-viewer>
</template>
<script>
任超 committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
import ElImageViewer from 'element-ui/packages/image/src/image-viewer'
export default {
  components: {
    ElImageViewer,
  },
  props: {
    urlList: {
      type: Array,
      default: function () {
        return []
      }
    }
  },
  data () {
    return {
      wrapperElem: null
    }
  },
  mounted () {
    this.$nextTick(() => {
      let wrapper = document.getElementsByClassName(
        'el-image-viewer__actions__inner'
      )
      let downImg = document.createElement('i')
      downImg.setAttribute('class', 'el-icon-download')
      wrapper[0].appendChild(downImg)
      if (wrapper.length > 0) {
        this.wrapperElem = wrapper[0]
        this.wrapperElem.addEventListener('click', this.hideCusBtn)
任超 committed
35
      }
任超 committed
36 37 38 39 40
    })
  },
  methods: {
    closeViewer () {
      this.$emit('close-viewer')
任超 committed
41
    },
任超 committed
42 43 44 45 46 47 48 49
    hideCusBtn (e) {
      let className = e.target.className
      if (className === 'el-icon-download') {
        let imgUrl = document.getElementsByClassName(
          'el-image-viewer__canvas'
        )[0].children[0].src
        this.downloadImage(imgUrl)
      }
任超 committed
50
    },
任超 committed
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
    downloadImage (imgUrl) {
      let tmpArr = imgUrl.split('/')
      let fileName = tmpArr[tmpArr.length - 1]
      window.URL = window.URL || window.webkitURL
      let xhr = new XMLHttpRequest()
      xhr.open('get', imgUrl, true)
      xhr.responseType = 'blob'
      xhr.onload = function () {
        if (this.status == 200) {
          //得到一个blob对象
          let blob = this.response
          let fileUrl = window.URL.createObjectURL(blob)
          let a = document.createElement('a')
            ; (document.body || document.documentElement).appendChild(a)
          a.href = fileUrl
          if ('download' in a) {
            a.download = fileName
          } else {
            a.setAttribute('download', fileName)
任超 committed
70
          }
任超 committed
71 72 73
          a.target = '_self'
          a.click()
          a.remove()
任超 committed
74
        }
任超 committed
75 76
      }
      xhr.send()
任超 committed
77
    },
任超 committed
78 79
  },
}
任超 committed
80 81 82
</script>

<style lang="scss" scoped>
任超 committed
83 84 85
/deep/ .el-image-viewer__close {
  color: #ffffff;
}
任超 committed
86
</style>