vue 预览word,excel,txt,pdf等文本

2 篇文章 0 订阅
2 篇文章 0 订阅

准备

npm install --save vue-pdf

代码

<template>
  <div>
    <div v-if="filetype === 'image'">
      <img :src="urlLink" alt="" />
    </div>
    <div v-else-if="filetype === 'office'">
      <!-- 分页 -->
      <div>
        <button @click="changePage(0)"></button>
        <span>{{ currentPage }}/{{ pageCount }}</span>
        <button @click="changePage(1)"></button>
      </div>
      <!--pdf  -->
      <div>
        <pdf
          :src="urlLink"
          :page="currentPage"
          @num-pages="pageCount = $event"
          @page-loaded="currentPage = $event"
          @loaded="loadPdfHandler"
        />
      </div>
    </div>
    <!-- excel -->
    <div v-else-if="filetype === 'excel'">
      <div v-html="exldata"></div>
    </div>
  </div>
</template>
<script>
import pdf from "vue-pdf";
export default {
  components: {
    pdf,
  },
  data() {
    return {
      filetype: "",
      exldata: [],
      // 文件服务地址
      fsServerPath: "http://loacalhost:8080",
      currentPage: 1,
      pageCount: 1,
      txtPre: "",
      urlLink: "",
      fileStudyPage: 1, //已浏览的页码
    };
  },
  methods: {
    // record 从父页面传过来obj,包含fileName,fileId
    open(record) {
      let fileTypeName;
      if (record.fileName) {
        fileTypeName = record.fileName
          .substring(record.fileName.indexOf(".") + 1, record.fileName.length)
          .toLowerCase();
      }
      this.urlLink = "";
      if (
        fileTypeName === "png" ||
        fileTypeName === "jpg" ||
        fileTypeName === "jpeg"
      ) {
        this.urlLink = this.fsServerPath + record.fileId;
        this.filetype = "image";
      } else if (
        fileTypeName === "pdf" ||
        fileTypeName === "doc" ||
        fileTypeName === "docx"
      ) {
        this.currentPage = 1;
        this.urlLink = this.fsServerPath + record.fileId;
        this.filetype = "office";
        this.loadPdfHandler();
      } else {
        this.filetype = "excel";
        this.urlLink = this.fsServerPath + record.fileId;
        this.$http.get(this.urlLink).then((res) => {
          this.exldata = `<!DOCTYPE html><html lang="en"><head>
              <meta charset="utf-8">
              <meta http-equiv="Content-Type" content="text/html">
            </head>
            <body>
            ${res}
            </body>
          </html>`;
        });
      }
    },
    changePage(num) {
      if (num === 0 && this.currentPage > 1) {
        this.currentPage--;
      }
      if (num === 1 && this.currentPage < this.pageCount) {
        this.currentPage++;
      }
    },
    loadPdfHandler() {
      this.currentPage = this.fileStudyPage || 1;
    },
  },
};
</script>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值