常用文件格式通过文件名称后缀匹配图片

代码片段

export default {
  data () {
    return {
      fileTypes: {
        imgTypes: ['png', 'jpg', 'jpeg', 'jfif', 'gif', 'PNG', 'JPG', 'JPEG', 'JFIF', 'GIF'],
        docTypes: ['doc', 'docx'],
        zipTypes: ['zip'],
        rarTypes: ['rar'],
        textTypes: ['txt'],
        pdfTypes: ['pdf'],
        excelTypes: ['xlsx', 'xls'],
        pptTypes: ['ppt', 'pptx'],
        videoTypes: ['mp4'],
        audioTypes: ['mp3']
      }
    };
  },
  methods: {
    fileIcon (fileName) {
      const keys = Object.keys(this.fileTypes);
      for (let i = 0, j = keys.length; i < j; i++) {
        for (let k = 0, l = this.fileTypes[keys[i]].length; k < l; k++) {
          if (this.fileTypes[keys[i]].some(val => fileName.includes(val))) {
            let imgUrl = '';
            switch (keys[i]) {
              case 'imgTypes':
                imgUrl = require('@/assets/imgs/png-icon.png');
                break;
              case 'docTypes':
                imgUrl = require('@/assets/imgs/doc-icon.png');
                break;
              case 'zipTypes':
                imgUrl = require('@/assets/imgs/zip-icon.png');
                break;
              case 'rarTypes':
                imgUrl = require('@/assets/imgs/rar-icon.png');
                break;
              case 'textTypes':
                imgUrl = require('@/assets/imgs/txt-icon.png');
                break;
              case 'pdfTypes':
                imgUrl = require('@/assets/imgs/pdf-icon.png');
                break;
              case 'excelTypes':
                imgUrl = require('@/assets/imgs/excel-icon.png');
                break;
              case 'pptTypes':
                imgUrl = require('@/assets/imgs/ppt-icon.png');
                break;
              case 'videoTypes':
                imgUrl = require('@/assets/imgs/video-icon.png');
                break;
              case 'audioTypes':
                imgUrl = require('@/assets/imgs/audio-icon.png');
                break;
              default:
                imgUrl = require('@/assets/imgs/doc-icon.png');
            }
            return imgUrl;
          }
        }
      }
    }
  }
};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值