文件阅览功能的实现(适用于word、pdf、Excel、ppt、png...)

需求描述:

需要一个组件,同时能预览多种类型文件,一种类型文件可有多个的文件。

看过各种博主的方案,其中最简单的是利用第三方地址进行预览解析(无需任何插件);

这里推荐三个地址:       

        @params 微软解析地址

        @url    https://view.officeapps.live.com/op/view.aspx?src=资料地址

        @params docx云服务解析 大小限制 50MB

        @url    http://view.xdocin.com/xdoc?_xdoc=资料地址

        @params 豆瓣在线预览解析  

        @url    www.xdocin.com/xdoc?_func=to&_format=html&_cache=1&_xdoc=资料地址

 效果图:

已测试,因为单线程原因,可能有些文件无法同时展现,后期还会优化

 注意:操作的文件必须是公网能访问得到的,否则无法被解析;

组件代码:

<!--多文件类型预览-->
<template>
  <el-dialog
    class="filedialog"
    title="文件预览"
    append-to-body
    :visible.sync="dialogVisible"
    close-on-press-escape
    @close="close"
    width="70%"
  >
    <span slot="title" class="dialog-footer">
      <div class="title">
        <span>文件预览</span>
      </div>
      <div class="select_box">
        <el-select
          v-model="fileType"
          placeholder="请选择文件类型"
          @change="selectChange"
          clearable
          transfer="true"
          :popper-append-to-body="false"
          popper-class="select-popper"
        >
          <el-option
            v-for="(item, index) in fileTypes"
            :key="index"
            :label="`文件类型` + (index + 1) + item"
            :value="item"
            >{{ `文件类型` + (index + 1) + item }}</el-option
          >
        </el-select>
      </div>
    </span>
    <template v-if="onImg">
      <iframe
        v-for="(item, index) in fileListInfo"
        :src="item"
        frameborder="0"
        width="100%"
        height="650"
        class="iframe"
      ></iframe>
    </template>
    <template v-else>
      <div class="block" :style="{ height: '600px' }">
        <!-- <span class="demonstration">默认 Hover 指示器触发</span> -->
        <el-carousel height="600px">
          <el-carousel-item v-for="item in fileListInfo" :key="item">
            <h3 class="small">
              <img
                :src="item"
                alt=""
                :style="{ width: '100%', height: 'auto' }"
              />
            </h3>
          </el-carousel-item>
        </el-carousel>
      </div>
    </template>
  </el-dialog>
</template>

<script>
export default {
  name: "file_Preview",
  data() {
    return {
      imgVisibleURL: "",
      onImg: true,
      dialogVisible: false,
      fileTypes: [],
      fileType: "",
      fileListInfo: null,
    };
  },
  props: {
    fileDialogVisible: {
      type: Boolean,
    },
    filePreview: {
      type: String,
      default: "",
    },
  },
  watch: {
    fileDialogVisible: {
      handler(newVal, oldVal) {
        this.dialogVisible = newVal;
      },
      deep: true,
      immediate: true,
    },
    filePreview: {
      handler(newVal, oldVal) {
        if (newVal) {
          this.imgVisibleURL = newVal;
          //类型收集
          this.fileTypes = this.conversion(newVal);
          //默认选中
          this.$nextTick(() => {
            this.fileType = this.fileTypes[0];
            this.selectChange(this.fileTypes[0]);
          });
        }
      },
      deep: true,
      immediate: false,
    },
  },
  created() {},
  mounted() {},
  methods: {
    /**
     *  @params 微软解析地址
        @url    https://view.officeapps.live.com/op/view.aspx?src=
        @params docx云服务解析 大小限制 50MB
        @url    http://view.xdocin.com/xdoc?_xdoc=
        @params 豆瓣在线预览解析  
        @url    www.xdocin.com/xdoc?_func=to&_format=html&_cache=1&_xdoc=
     */
    conversion(str) {
      str.split(",").forEach((item, index) => {
        let extIndex = item.lastIndexOf(".");
        let ext = item.substr(extIndex);
        this.fileTypes.push(ext);
      });
      let unique = (arr) => {
        return Array.from(new Set(arr));
      };
      return unique([...this.fileTypes]);
    },
    selectChange(e) {
      const fileDataList = this.fileFilter(this.imgVisibleURL.split(","), e);
      if (
        e == ".xls" ||
        e == ".doc" ||
        e == ".docx" ||
        e == ".xlsx" ||
        e == ".detx" ||
        e == ".pptx" ||
        e == ".ppt" 
      ) {
        //需二次处理
        this.onImg = true;
        this.fileListInfo = fileDataList;
        this.fileListInfo = this.fileListInfo.map((url, index) => {
          return `https://view.officeapps.live.com/op/view.aspx?src=${url}`;
        });
      } else if (e == ".png" || e == ".jpg" || e == ".webp" || e == ".gif") {
        //图片类型直接
        this.onImg = false;
        this.fileListInfo = fileDataList;
      } else if (e == ".pdf") {
        //.pdf类型不需要二次处理
        this.onImg = true;
        this.fileListInfo = fileDataList;
      }
    },
    fileFilter(arr, callback) {
      let urlArr = [];
      arr.forEach((item, index) => {
        let extIndex = item.lastIndexOf(".");
        let ext = item.substr(extIndex);
        if (ext == callback) {
          urlArr.push(item);
        }
      });
      return urlArr;
    },
    close() {
      this.$emit("fileCloseDialog", false);
      this.fileTypes = [];
      this.fileListInfo = null;
      this.fileType = "";
    },
  },
};
</script>

<style lang="less" scoped>
.filedialog {
  width: 100%;
  height: 100%;
  z-index: 9999 !important;
  ::v-deep .dialog-footer {
    height: 50px;
    font-size: 19px;
    display: flex;
    align-items: center;
    position: relative;
    .select_box {
      position: absolute;
      right: 100px;
    }
  }

  ::v-deep .el-dialog {
    opacity: 0.9;

    .el-dialog__body {
      height: 700px;
      margin: 0;
      padding: 15px;
      background: #00132f;
      overflow: auto;

      // 滚动条整体部分
      &::-webkit-scrollbar {
        width: 6px;
        height: 6px;
      }
      // 滚动条的轨道的两端按钮,允许通过点击微调小方块的位置。
      &::-webkit-scrollbar-button {
        display: none;
      }
      // 滚动条的轨道(里面装有Thumb)
      &::-webkit-scrollbar-track {
        background: transparent;
      }
      // 滚动条的轨道(里面装有Thumb)
      &::-webkit-scrollbar-track-piece {
        background-color: transparent;
      }
      // 滚动条里面的小方块,能向上向下移动(或往左往右移动,取决于是垂直滚动条还是水平滚动条)
      &::-webkit-scrollbar-thumb {
        background: #fff;
        cursor: pointer;
        border-radius: 4px;
      }
      // 边角,即两个滚动条的交汇处
      &::-webkit-scrollbar-corner {
        display: none;
      }
      // 两个滚动条的交汇处上用于通过拖动调整元素大小的小控件
      &::-webkit-resizer {
        display: none;
      }
    }
    .el-dialog__header {
      background: #00132f;
      color: white;
    }
  }
}

</style>

方法还是有很多的,下面给大家推荐几个还不错的网站;

可参考:

vue在线预览word、excel、pdf、txt、图片的相关资料,

https://www.jb51.net/article/266520.html

vue集成Luckyexcel实现在线编辑Excel,可自行导入,也可从服务器端获取:

https://blog.csdn.net/weixin_45000975/article/details/121856816

希望对大家有所帮助,如有不妥,多多包涵

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: DICOM(Digital Imaging and Communications in Medicine)是一种用于医学图像传输和存储的标准文件格式。CT(计算机断层扫描)和MRI(磁共振成像)是常见的医学影像检查方法,CT/MRI.zip是一个包含CT和MRI影像文件的压缩文件。 其中,CT(计算机断层扫描)是一种医学检查技术,通过使用x射线和计算机技术,可以生成横断面的体内结构图像。CT图像可以用于发现和诊断多种疾病,如脑部肿瘤、心脏问题和骨折等。CT图像文件遵循DICOM标准格式,以确保无论在何种设备上都可以读取和解释。 MRI(磁共振成像)是一种使用强磁场和无害的无线电波来产生体内结构图像的医学检查技术。MRI图像可以提供更详细的组织结构信息,对于检测和诊断各种疾病,如肌肉骨骼问题、脊髓损伤和肿瘤等非常有用。与CT一样,MRI图像文件也采用DICOM格式,以确保其在不同设备和软件中的兼容性和可读性。 CT/MRI.zip是一个压缩文件,其中包含CT和MRI图像文件。这种压缩文件可以方便地传输和存储大量的医学影像文件,减少存储空间和传输时间。通过解压缩CT/MRI.zip文件,医生和医学专业人员可以轻松地访问和查看这些CT和MRI图像文件,以进行诊断、治疗规划和疾病监控等工作。 ### 回答2: DICOM(Digital Imaging and Communications in Medicine)是一种用于医学影像文件传输和存储的国际标准。它规定了医学影像文件的格式、结构和通信方式,使得不同厂商的医疗设备和软件能够互相兼容和交互。 CT(Computed Tomography)和MRI(Magnetic Resonance Imaging)是常见的医学影像检查技术。CT利用X射线进行影像生成,能够提供高分辨率的骨骼和软组织图像。MRI则利用磁场和无害的射频波生成影像,对软组织有更好的对比度和解剖显示。 "ct/mri.zip"是一个DICOM医学影像文件的压缩包。这个压缩包中包含了CT和MRI检查产生的DICOM格式的影像文件。压缩包形式便于传输和存储影像文件,同时也保护了文件的完整性和安全性。 解压缩"ct/mri.zip"后,您能够打开包含的DICOM影像文件,并通过DICOM阅览器或者专业的医学影像处理软件查看这些影像。通过这些软件,医生、技术员和研究人员能够分析和诊断这些影像,帮助识别疾病、监测治疗进展以及进行科学研究。 通过DICOM标准和DICOM文件格式的应用,医学影像数据可以在不同的医疗设备和软件之间进行传输和共享,无论是在同一家医院的不同科室之间,还是在不同医疗机构之间。这有助于医疗机构间的合作以及对医学影像数据的更好管理和利用,有助于提高医疗服务的质量和效率。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值