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

  1. 直接代码
javascript 复制代码
   //图片直接用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. 粘贴一下vue组件代码可直接使用
javascript 复制代码
<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>
相关推荐
GIS之路1 分钟前
ArcGIS Pro 中的 Python 入门
前端
树獭非懒10 分钟前
告别繁琐多端开发:DivKit 带你玩转 Server-Driven UI!
android·前端·人工智能
兆子龙42 分钟前
当「多应用共享组件」成了刚需:我们从需求到模块联邦的落地小史
前端·架构
Qinana43 分钟前
从代码到智能体:MCP 协议如何重塑 AI Agent 的边界
前端·javascript·mcp
Wect1 小时前
LeetCode 130. 被围绕的区域:两种解法详解(BFS/DFS)
前端·算法·typescript
不会敲代码11 小时前
从入门到进阶:手写React自定义Hooks,让你的组件更简洁
前端·react.js
用户5433081441941 小时前
拆完 Upwork 前端我沉默了:你天天卷的那些技术,人家根本没用
前端
洋洋技术笔记1 小时前
Vue实例与数据绑定
前端·vue.js
Marshall1511 小时前
zzy-scroll-timer:一个跨框架的滚动定时器插件
前端·javascript
明月_清风3 小时前
打字机效果优化:用 requestAnimationFrame 缓冲高频文字更新
前端·javascript