文件预览(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>
相关推荐
Hoshizola11 分钟前
uniapp与蓝牙设备连接详细步骤
前端·uni-app
优雅格子衫16 分钟前
uniapp 拍照相册选取后超级好用的裁剪组件,增加水印完全自定义
开发语言·前端·javascript·uni-app·vue
周末也要写八哥17 分钟前
Visual C++6.0下载安装流程及PDF学习手册资源
c++·学习·pdf
Dxy123931021620 分钟前
HTML如何写鼠标事件
前端·html·计算机外设
优化控制仿真模型28 分钟前
2026初中英语考纲词汇表(1600词)PDF电子版
经验分享·pdf
AI砖家33 分钟前
前端 JavaScript 异步处理全方案详解:从回调到 Observable
开发语言·前端·javascript
用户7138742290038 分钟前
构建现代 Web 应用的令牌安全体系:Refresh Token Rotation、HttpOnly Cookie 与 Grace Period 全解析
前端
之歆1 小时前
Day21_电商详情页核心技术实战:从LESS预处理到复杂交互实现
开发语言·前端·javascript·css·交互·less
海鸥两三1 小时前
基于 Vue 3 + 高德地图的网格规划系统实战(有源码)
前端·javascript·vue.js
丷丩1 小时前
MapLibre GL JS第11课:获取鼠标指针坐标
前端·javascript·gis·地图·mapbox·maplibre gl js