Vue3使用kkFileView预览文件pdf

kkFileView - 在线文件预览kkFileView官网 - kkFileView使用Spring Boot搭建,易上手和部署,基本支持主流办公文档的在线预览,如doc,docx,Excel,pdf,txt,zip,rar,图片等等https://kkfileview.keking.cn/zh-cn/docs/usage.html业务场景:需要点击按钮,弹出弹框预览pdf文件

这里使用的是3.x.x 版本,需要下载base64

3.x.x 版本 # 示例

复制代码
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/base64.min.js"></script>

var originUrl = 'http://127.0.0.1:8080/filedownload?fileId=1'; //要预览文件的访问地址
var previewUrl = originUrl + '&fullfilename=test.txt'
window.open('http://127.0.0.1:8012/onlinePreview?url='+encodeURIComponent(Base64.encode(previewUrl)));

配置baseUrl,如果是生产环境,走 location.origin 浏览器的当前地址,它包含了协议、域名和端口号,否则走项目配置中设置的环境变量 VITE_APP_API_URL 的值。

替换示例的文件地址,打开弹窗时把地址传给弹窗,弹窗里使用iframe

javascript 复制代码
// 预览
    const handleClickOpenPreview = (row) => {
      if (row.fileState === '无') return
      const baseUrl = import.meta.env.PROD ? location.origin : import.meta.env.VITE_APP_API_URL
      
      // const originUrl = baseUrl + '/hs/document/downloadWord' + '?fileName=010001_20231015_1697332623776.docx'
      // const previewUrl = originUrl + '&fullfilename=010001_20231015_1697332623776.docx'
      
      // window.open('http://100.100.120.147:8012/preview/onlinePreview?url=' + encodeURIComponent(Base64.encode(previewUrl)));
      // window.open(baseUrl + '/preview/onlinePreview?url=' + encodeURIComponent(Base64.encode(previewUrl)));
      
      const originUrl = baseUrl + '/hs/document/downloadWord' + `?fileName=${row.fileName}`
      const previewUrl = originUrl + `&fullfilename=${row.fileName}`
      const url = baseUrl + '/preview/onlinePreview?url=' + encodeURIComponent(Base64.encode(previewUrl))
      state.PreViewFileDialogRef.open(row, url) // 把url传给弹窗
    }

弹窗页

javascript 复制代码
<template>
  <PatrolDialog :show="show" @close="show = false" DeleteFooterBtn width="1370px" top="5vh">
    <template #title>
      <div class="dialog-title"><img :src="weatherStationIcon" />预览</div>
    </template>

    <div class="formPart">
      <el-dropdown trigger="click" @command="handleClickExport" size="small">
        <span class="el-dropdown-link">
          <i class="icon iconfont icon-daochu" style="color: var(--gdky-primary-color);"></i>
        </span>
        <template #dropdown>
          <el-dropdown-menu>
            <el-dropdown-item command="word">word</el-dropdown-item>
            <el-dropdown-item command="pdf">pdf</el-dropdown-item>
          </el-dropdown-menu>
        </template>
      </el-dropdown>
      <!-- 在弹窗中显示的内容 -->
      <iframe :src="pageUrl" width="100%" height="800px" class="file"></iframe>
    </div>
  </PatrolDialog>
</template>

<script>
import { reactive, toRefs, computed, watch } from 'vue'
import PatrolDialog from '@/views/Components/PatrolDialog.vue'
import weatherStationIcon from '@/assets/imgs/title_img.png'
import { exportWord, exportPdf } from '@/api/PlatTool/CustomModules/HeatSourceDispatch/index'

export default {
  components: {
    PatrolDialog
  },
  setup(_, { emit }) {

    const state = reactive({
      show: false,
      weatherStationIcon, // icon
      curRow: null,
      pageUrl: "",
    })


    const methods = {
      // 打开弹窗
      async open(curRow, url) {
        state.show = true;
        state.curRow = curRow;
        state.pageUrl = url
      },

      // 导出
      handleClickExport(command) {
        if (command === "word") {
          exportWord({ fileName: state.curRow.fileName })
        } else {
          exportPdf({ fileName: state.curRow.pdfFileName })
        }
      }
    }

    return {
      ...toRefs(state),
      ...methods,
    }
  }
}
</script>
<style lang='less' scoped>
.dialog-title {
  display: flex;
  font-size: 16px;
  font-family: MicrosoftYaHeiSemibold;
  color: var(--gdky-main-content-color);
  line-height: 24px;
  padding-bottom: var(--base-padding);

  img {
    margin-right: 8px;
  }
}

.formPart {
  display: flex;
  flex-direction: column;
  flex: 1;
  height: 100%;

  .file {
    display: flex;
    justify-content: center;
    align-items: center;
    margin-left: -15px;
    position: relative;
  }

  .el-dropdown-link {
    position: absolute;
    top: 5px;
    right: 88px;
    z-index: 999;
  }
}
</style>

<style lang="less">
.g-dark {
  .formPart {
    .el-dropdown-link {}
  }
}
</style>
相关推荐
apcipot_rain4 小时前
【应用密码学】实验五 公钥密码2——ECC
前端·数据库·python
油丶酸萝卜别吃5 小时前
OpenLayers 精确经过三个点的曲线绘制
javascript
ShallowLin5 小时前
vue3学习——组合式 API:生命周期钩子
前端·javascript·vue.js
Nejosi_念旧5 小时前
Vue API 、element-plus自动导入插件
前端·javascript·vue.js
互联网搬砖老肖5 小时前
Web 架构之攻击应急方案
前端·架构
pixle05 小时前
Vue3 Echarts 3D饼图(3D环形图)实现讲解附带源码
前端·3d·echarts
麻芝汤圆6 小时前
MapReduce 入门实战:WordCount 程序
大数据·前端·javascript·ajax·spark·mapreduce
juruiyuan1118 小时前
FFmpeg3.4 libavcodec协议框架增加新的decode协议
前端
Peter 谭8 小时前
React Hooks 实现原理深度解析:从基础到源码级理解
前端·javascript·react.js·前端框架·ecmascript
周胡杰9 小时前
鸿蒙接入flutter环境变量配置windows-命令行或者手动配置-到项目的创建-运行demo项目
javascript·windows·flutter·华为·harmonyos·鸿蒙·鸿蒙系统