文件预览(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>
相关推荐
梦想很大很大6 分钟前
一句“你好”用了 5671 Token:我在 Workrun 中追查 Agent 隐性成本的过程
前端·人工智能·后端
SameX12 分钟前
HarmonyOS ArkTS 做动画 FPS 自适应降级的滞后窗口做法
前端
用户978876053904815 分钟前
Canvas 处理大图时如何不卡住主线程:一次浏览器端批量压缩的调度实践
前端
鶴哥只手遮天18 分钟前
# 《Electron 45.2.3 内核解剖》第 6 章 Node.js 集成:两个事件循环的婚姻
前端·electron·开源
bai27319 分钟前
别再手动刷后台了:用原生 CDP 给 4 个内容平台做登录态保活心跳
前端
程序员大阳29 分钟前
副队长ES6+教程(4)--拓展运算符
javascript·vue.js·es6·收集·拓展运算符·展开
工程小白菜37 分钟前
AI 生成的代码谁来验收?从 Copilot 到 WorkBuddy 的三个实践
前端·ai编程
我不会起名字3221 小时前
仅 Web 主动 ping:30 秒读超时与各平台保活
前端·后端·websocket·go·即时通讯·im·故障排查
徐小夕1 小时前
CADViewer-Skill:让 Agent 原生支持 DWG 解析、可视化图统计、图纸导出
前端·vue.js·github
Mh1 小时前
AI 的模型能力越来越强,为什么我的编码能力越来越弱?
前端·javascript·后端