当前位置: 首页 > 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.xdnf.cn/news/1270621.html

相关文章:

  • 【linux基础】Linux命令提示符解析与操作指南
  • 如何在 Ubuntu 24.04 LTS Linux 上安装 Azure Data Studio
  • 编译技术的两条演化支线:从前端 UI 框架到底层编译器的智能测试
  • “自动报社保 + 查询导出 ” 的完整架构图和 Playwright C# 项目初始化模板
  • 基于IPD体系的研发项目范围管理
  • 【渲染流水线】[几何阶段]-[几何着色]以UnityURP为例
  • 202506 电子学会青少年等级考试机器人三级器人理论真题
  • 《算法导论》第 15 章 - 动态规划
  • FreeRTOS源码分析五:资源访问控制(一)
  • SOLi-LABS Page-3 (Stacked injections) --39-53关
  • OpenAI 的最新 AI 模型 GPT-5 现已在 GitHub Models 上提供!
  • 如何在 Windows 下使用 WSL 安装 Ubuntu 并配置国内镜像
  • 神经网络-local minima and saddle point
  • FFMPEG将H264转HEVC时,码率缩小多少好,以及如何通过SSIM(Structural Similarity Index结构相似性指数)衡量转码损失
  • 使用Navicat备份数据库MySQL、PostGreSQL等
  • Meta AI水印计划的致命缺陷——IEEE Spectrum深度文献精读
  • (nice!!!)(LeetCode 面试经典 150 题) 146. LRU 缓存 (哈希表+双向链表)
  • 力扣热题100------70.爬楼梯
  • 如何解决 Vue 项目启动时出现的 “No such module: http_parser” 错误问题
  • Cherryusb UAC例程对接STM32内置ADC和DAC播放音乐和录音(中)=>UAC+STM32 ADC+DAC实现录音和播放
  • traceroute命令调试网络
  • C++高频知识点(十七)
  • 《Resolving tissue complexity by multimodal spatial omics modeling with MISO》
  • 9. 堆和栈有什么区别
  • Vitalik谈以太坊:ETH财库储备策略“有益且有价值”
  • Kotlin 协程线程切换机制详解
  • AG32cpld实现一个UartTx“外设”
  • 智慧能源设备巡检缺陷漏检率↓76%:陌讯多模态融合算法实战解析
  • Android适配最新SplashScreen方案:让启动页不再“翻车“
  • webrtc弱网-BandwidthQualityScaler 源码分析与算法原理