• 文件预览(img,doc,pdf,xls)


    1. 直接代码
       //图片直接用img标签展示
     <img width="500" v-if="showImg" height="500" :src="url" />
     //非图片用iframe标签展示
     <iframe v-else :src="url" width="100%" height="100%" frameborder="0"></iframe>
    
    • 1
    • 2
    • 3
    • 4
    1. 粘贴一下vue组件代码可直接使用
    <template>
      <el-dialog :title="title" :visible.sync="open" top="3vh" append-to-body :custom-class="`doc-view ${handleClass}`"
        :width="width">
        <div style="height: calc(100% - 100px);width: 100%;">
          <img width="500" v-if="showImg" height="500" :src="url" />
          <iframe v-else :src="url" width="100%" height="100%" frameborder="0"></iframe>
        </div>
        <div style="display: flex;justify-content:flex-end;margin-top: 10px;">
          <slot></slot>
        </div>
    
      </el-dialog>
    </template>
    
    <script>
    /* oss文件地址前缀 */
    import website from "@/const/website.js";
    export default {
      data() {
        return {
          open: false,
          width: "",
          title: "",
          url: "",
          isWordSuffer: ["doc", "docx"],
          isImgSuffer: ["png", "jpg", "jpeg"],
          isPdfSuffer: ["pdf"],
          isExcelSuffer: ["xls", "xlsx"],
          showImg: false,
        };
      },
      computed: {
        handleClass() {
          if (this.showImg) return "";
          return "is_iframe";
        },
      },
      methods: {
        show(url, suffName = "jpg") {
          /* 组织线上展示的文件地址 */
          const preview_url = website.ossUrl + url.slice(url.lastIndexOf("/"));
    
          const { isWordSuffer, isImgSuffer, isPdfSuffer, isExcelSuffer } = this;
          /* 图片 */
          if (isImgSuffer.includes(suffName)) {
            this.showImg = true;
            this.title = "图片预览";
            this.width = "600px";
            this.url = preview_url;
          } else {
            this.showImg = false;
            this.width = "960px";
            /* pdf */
            if (isPdfSuffer.includes(suffName)) {
              this.title = "PDF预览";
              this.url = preview_url;
              /* word预览需要拼接前缀 */
            } else if (isWordSuffer.includes(suffName)) {
              this.title = "Word预览";
              this.url = `https://view.officeapps.live.com/op/view.aspx?src=${preview_url}`;
            } else if (isExcelSuffer.includes(suffName)) {
              /* excel预览需要拼接前缀 */
              this.width = "1200px";
              this.title = "Excel预览";
              this.url = `https://view.officeapps.live.com/op/view.aspx?src=${preview_url}`;
            }
          }
          this.open = true;
        },
    
      },
    };
    </script>
    
    <style lang="scss">
    .doc-view {
      .el-dialog__body {
        // display: flex;
        // justify-content: center;
      }
    }
    
    .doc-view.is_iframe {
      .el-dialog__body {
        height: 75vh;
      }
    }
    </style>
    
    
    • 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
    • 30
    • 31
    • 32
    • 33
    • 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
  • 相关阅读:
    React(Js)学习
    IDM的实用功能
    gRPC之gateway集成swagger
    mysql锁(全局锁、表锁、行锁、页锁、排他锁、共享锁)
    Web3:价值投资的范式转移
    极客日报:美团APP被曝连续24小时定位;清华校友吴旻当选IEEE SPS首位华裔女主席;VS Code 1.61发布
    Linux系统编程——文件的打开及创建
    ensp——防火墙安全策略配置实验
    什么是现场服务管理系统(FSM)?有什么好处?
    关于如何系统的学习python
  • 原文地址:https://blog.csdn.net/weixin_45580774/article/details/136524135