文件预览(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>
相关推荐
奇树谦几秒前
离线英文 PDF 翻译工具使用说明
pdf
SwJieJie3 分钟前
Day1 从 0 搭建 VueDemo Web Admin 项目环境:技术栈、插件链与自动化脚本全解析
前端·vue.js·学习
wordbaby5 分钟前
React 自定义 Hook 实践:如何优雅管理复杂列表的筛选状态?
前端
xuankuxiaoyao6 分钟前
Vue.js 插槽、作用域插槽、商品、阶段案例
android·vue.js·flutter
EF@蛐蛐堂7 分钟前
TanStack NPM攻击 揭秘及应对方案
前端·vue.js·npm·安全威胁分析
xiaoliuliu123458 分钟前
Mendeley 2.1 文献管理软件安装教程:文献管理+PDF标注
pdf
恋猫de小郭10 分钟前
终于,Flutter 修复 Android 中文字体异常,但是很草台,不知怎么吐槽
android·前端·flutter
Cobyte13 分钟前
11.响应式系统演进:深入剖析 computed 实现原理与性能优化实践(Vue3.3)
前端·javascript·vue.js
_Evan_Yao14 分钟前
计算机大一新生如何选择方向(前端/后端/AI/运维)?
运维·前端·人工智能·后端
ZC跨境爬虫14 分钟前
跟着MDN学HTML_day_46:(HTMLCollection与NodeList)
前端·javascript·ui·html·音视频