文件预览(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>
相关推荐
Bigger10 分钟前
堆了 20 套主题、10+ 组件,用户还是用不出来?我给设计 Skill 加了「自动驾驶」
前端·ai编程·视觉设计
贾天佑忆月 迷失的昵26 分钟前
.NET Web开发技术简单整理
前端·.net
布兰妮甜33 分钟前
Vue 路由进阶:路由守卫权限控制、动态路由、懒加载、路由缓存
vue.js·懒加载·动态路由·路由守卫权限控制·路由缓存
Cobyte1 小时前
Vite & Rollup 插件开发实践
前端·javascript·vue.js
朝阳391 小时前
react19【系列实用教程】实用组件封装
开发语言·前端·javascript
IT_陈寒1 小时前
React的useEffect依赖数组把我坑惨了,原来这样写才靠谱
前端·人工智能·后端
恋猫de小郭1 小时前
Flutter hit,一个可以灵活控制溢出点击的第三方包
android·前端·flutter
古韵1 小时前
小程序上传进度条,还要自己监听 onProgressUpdate 吗?
前端·javascript·uni-app
古韵1 小时前
axios 已经跑得好好的,还要不要上 alova?
前端·javascript·axios
噗噗121 小时前
企业微信侧边栏应用与前端 JS-SDK 的深度整合指南
前端·javascript·企业微信