fileItem.vue 3.42 KB
Newer Older
wangcong committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
<template>
  <div :style="{width:fileWidth}" class="file">
    <!-- <img :src="require(`../assets/img/TXT.png`)" class="file-icon"> -->
    <span class="file-icon">
      <svg class="icon" aria-hidden="true">
        <use :xlink:href="`#icon-_CGfujian${fileType}`"></use>
      </svg>
    </span>
    <div class="file-content">
      <div class="file-name">{{file.fileName}}</div>
      <div class="file-createdtime">{{file.createTime | filterCreateTime}}</div>
    </div>
    <Icon class="file-download" type="md-download" @click="handleDownload(file)" />
  </div>
</template>
<script>
export default {
  props: {
    file: Object,
    fileWidth: {
      type: String,
      default: '100%'
    }
  },
  computed: {
    fileType() {
      let type = ['bmp','ppt','txt','excel','zip','pdf','word','rar','jpg','gif','png']
      if (type.includes(this.file.mimeType)) {
        return this.file.mimeType
袁成 committed
30 31 32 33
      } else if (['docx', 'doc', 'docm'].includes(this.file.mimeType)) {
        return 'word'
      } else if (['xls', 'xlsx'].includes(this.file.mimeType)) {
        return 'excel'
wangcong committed
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
      } else {
        return 'tongyongtupian'
      }
    }
  },
  data() {
    return {
    }
  },
  filters: {
    filterCreateTime(val) {
      return val.substr(0, 10)
    },
  },
  methods: {
    handleDownload(list) {
      GMS.$http
        .get(`/common/attachment/${list.id}`, { responseType: 'blob' })
        .then((res) => {
          let blob = new Blob([res.data], {
            type: 'multipart/form-data;charset=utf-8',
          })
          //对文件名乱码转义--【Node.js】使用iconv-lite解决中文乱码
          let iconv = require('iconv-lite')
          iconv.skipDecodeWarning = true //忽略警告
          let downloadElement = document.createElement('a')
          let href = window.URL.createObjectURL(blob) // 创建下载的链接
          downloadElement.href = href
          downloadElement.download = list.fileName // 下载后文件名
          document.body.appendChild(downloadElement)
          downloadElement.click() // 点击下载
          document.body.removeChild(downloadElement) // 下载完成移除元素
          window.URL.revokeObjectURL(href) // 释放掉blob对象
        })
    },
  },
}
</script>
<style lang="less" scoped>
    .file {
      min-height: 60px;
      width: 100%;
      min-width: 60px;
      background: rgba(83, 105, 216, 0.04);
      border-radius: 4px;
      padding: 10px;
      margin-bottom: 10px;
      &-icon {
        // width: 20px;
        // height: 20px;
        width: 40px;
        text-align: left;
        
        font-size: 25px;
        float: left;
      }
      &-content {
        text-align: left;
        display: inline-block;
        width: calc(100% - 70px);
        .file-name {
          font-size: 14px;
          font-weight: 400;
          height: 20px;
qiaoyanqi committed
98
          font-family: PingFangSC-Regular, PingFang SC, sans-serif;
wangcong committed
99 100 101 102 103 104 105 106 107
          color: #0f0f0f;
          line-height: 20px;
          overflow: hidden;
          white-space: nowrap;
          text-overflow: ellipsis;
        }
        .file-createdtime {
          height: 17px;
          font-size: 12px;
qiaoyanqi committed
108
          font-family: PingFangSC-Regular, PingFang SC, sans-serif;
wangcong committed
109 110 111 112 113 114 115 116 117 118 119 120 121 122
          font-weight: 400;
          color: #7f7f7f;
          line-height: 17px;
        }
      }
      &-download {
        float: right;
        color: #b8b8b8;
        font-size: 16px;
        margin-top: 13px;
        cursor: pointer;
      }
    }
</style>