当前位置: 首页 > news >正文

vue3上传的文件在线查看

1、npm install @vue-office/pdf vue-demi   安装依赖

2、npm install @vue-office/excel vue-demi  安装依赖

3、npm install @vue-office/docx vue-demi   安装依赖

4、编写一个通用组件,现在只支持 .docx,.xlsx,.pdf 格式的文件,其他文件渲染不成功

<template><el-dialog v-model="lookFileVisible" title="查看文件" width="70%" :before-close="handleClose"><vue-office-docxv-if="currentAttachment.type === 'docx'":src="DownloadImgServerUrl + currentAttachment.src"style="height: 50vh;"@rendered="renderedHandler"@error="errorHandler"/><iframe v-if="currentAttachment.type === 'pdf'" style="width:100%;height:50vh;" :src="currentAttachment.src" frameBorder="0"></iframe><vue-office-excel:src="DownloadImgServerUrl + currentAttachment.src"v-if="currentAttachment.type === 'xlsx'"style="height: 50vh;"@rendered="renderedHandler"@error="errorHandler"/></el-dialog>
</template><script  lang="ts" setup>
import VueOfficeDocx from '@vue-office/docx'
import VueOfficeExcel from '@vue-office/excel'
import '@vue-office/docx/lib/index.css'
import '@vue-office/excel/lib/index.css'
import { onMounted, ref, watch } from 'vue';
import Tool from '../../../../global';
import { FileCenterModel } from '../class/FileCenterModel';
import { ElMessage } from 'element-plus';
const UploadImgServerUrl = new Tool().UploadImgServerUrl
const DownloadImgServerUrl = new Tool().DownloadImgServerUrl
const props = defineProps({lookFileVisible: Boolean,infoLookFile: FileCenterModel
})
//defineEmits用于定义回调事件,里面是数组,可以定义多个事件
const emits = defineEmits(["CloselookFile"])
const handleClose = (done: () => void) => {emits("CloselookFile")
}
// const docx=ref('a7285e21-d108-4887-a7cf-04455e141003.docx');// 'http://xxx.com/test6.docx', //设置文档网络地址,可以是相对地址
// const excel=ref('cd0d46a9-971f-47c8-af5f-ba6e796d511e.xls');//: 'http://xxx.com/test3.xls', //设置文档网络地址,可以是相对地址
// const pdf=ref('');//: 'http://xxx.com/test1.pdf'
const currentAttachment = ref({type: 'docx',src: '',
})
//监听
watch(() => props.infoLookFile,(newInfo, oldInfo) => {// console.log(newInfo,'>>>>>>>>>>>>>>>>>>>>>>>')if (newInfo != undefined) {let currInfo: FileCenterModel = (JSON.parse(newInfo as any)) as FileCenterModelcurrentAttachment.value.type = currInfo.fileSuffix;currentAttachment.value.src = currInfo.fileUrl;} else {}}
);
const renderedHandler =() => {ElMessage({message: '文件渲染完成!',type: 'success',})console.log("渲染完成")
}
const errorHandler = (err) => {ElMessage({message: '文件渲染失败',type: 'error',})console.log("渲染失败",err)
}
onMounted(() => {})
</script>

5、调用

<template><lookFileVue :lookFileVisible="lookFileVisible" :infoLookFile="infoLookFile" @CloselookFile="CloselookFile"></lookFileVue>
</template><script lang="ts" setup>import { reactive, ref, onMounted, toRaw, watch } from 'vue'import lookFileVue from '../FileCenter/components/LookFile.vue'//查看文件
const lookFileVisible = ref(false)
const infoLookFile = ref()
const lookFile = (row: FileCenterModel) =>{// downfile(row)// down(row.fileUrl, row.fileName, row.id)// console.log("查看文件地址", row)lookFileVisible.value = trueinfoLookFile.value = JSON.stringify(row)
}
const CloselookFile = () => {lookFileVisible.value = falseinfoLookFile.value = undefined
}
</script>

http://www.dtcms.com/a/323221.html

相关文章:

  • Mistral Small 3.1 架构深度解析:高效小型模型的巅峰之作
  • 华数杯C题:可调控生物节律的LED光源研究——数学建模与Python实战
  • 应用层Http协议(1)
  • 大玄古籍制作软件【详细教程20:txt文档config自动化配置】,排版软件,自动排版,排版设计,个人出书,一键排版
  • MATLAB中文乱码的解决方法
  • 吴恩达机器学习笔记(4)—多变量线性回归:梯度下降(附代码)
  • STM32学习笔记6-TIM-2输出比较功能
  • Python(13) -- 面向对象
  • 智慧能源设备巡检缺陷漏检率↓76%:陌讯多模态融合算法实战解析
  • 设备点检系统二维码的应用
  • ISO5001能源管理体系认证的流程
  • 频域中的正弦波
  • Datawhale+AI夏令营_让AI读懂财报PDF task2深入赛题笔记
  • Python樱花树
  • TOMCAT笔记
  • 数据安全与隐私保护:企业级防护策略与技术实现
  • C#异步编程双利器:异步Lambda与BackgroundWorker实战解析
  • protoc 生成java文件命令,protoc生成js命令 protobuf 生成java文件命令,protobuf生成js文件命令
  • 深入理解Qt事件处理机制
  • Cursor手机版:一半是神,一半是坑
  • Linux软件编程:shell
  • pdf预览Vue-PDF-Embed
  • 定制客车系统票务管理系统功能设计
  • 深度学习入门Day8:生成模型革命——从GAN到扩散模型
  • Vue 3.6 Vapor模式完全指南:告别虚拟DOM,性能飞跃式提升
  • 大模型“涌现”背后的暗线——规模、数据、目标函数的三重协奏
  • 华为实验:SSH
  • P1890 gcd区间
  • 在 VS Code 或 Visual Studio 2022 上搭建 ESP32-CAM 开发环境
  • 微软将于 10 月停止混合 Exchange 中的共享 EWS 访问