纯前度(vue)实现对pdf\mp4\png\jpg\jpegxls\doc\txt文件预览,无需要转化

server/2025/2/13 16:11:30/

我们知道要想在前端预览文件,可以通过浏览器自带的预览功能,我们只要window.open(ulr) 就可以实现对文件的预览,但是对于一些特殊的文件,目前很多浏览器的预览功能还是不支持的,今天我们就在纯前端来实现对这些文件的预览。

首先,我们需要使用到vue的两个组件:VueOfficeExcelVueOfficeDocx
下面直接看代码:

<template><div class="container"><iframe v-if="currFileType==='pdf' || currFileType==='mp4'" class="file" :src="$utils.getFullFilePathHM(currentRow.attPath)" type="application/x-google-chrome-pdf" /><img v-else-if="['png','jpg','jpeg'].includes(currFileType)" class="file" :src="$utils.getFullFilePathHM(currentRow.attPath)" style="object-fit: contain;" alt=""><vue-office-docx v-else-if="currFileType==='docx'" :src="$utils.getFullFilePathHM(currentRow.attPath)" /><div v-else-if="currFileType==='txt'" ref="txt" class="txt" /><vue-office-excel v-else-if="currFileType==='xls' || currFileType==='xlsx'" :src="$utils.getFullFilePathHM(currentRow.attPath)" /><div v-else class="empty-content"><p>文件格式暂不支持预览</p></div></div>
</template><script>
// 引入VueOfficeExcel组件
import VueOfficeExcel from '@vue-office/excel'
// 引入VueOfficeDocx组件
import VueOfficeDocx from '@vue-office/docx'
// 引入相关样式
import '@vue-office/excel/lib/index.css'
import '@vue-office/docx/lib/index.css'import { downloadFileNew } from '@/api/sponge-control/common-hm'
export default {name: 'PreviewFile',components: {VueOfficeExcel,VueOfficeDocx},props: {currentRow: {type: Object,default: () => {}}},data() {return {}},computed: {currFileType() {const arr = this.currentRow.attPath?.split('.')return arr && arr[arr.length - 1] || ''}},watch: {'currentRow.attPath': {handler() {if (this.currFileType === 'txt') {this.$nextTick(async() => {const res = await downloadFileNew({ fileName: this.currentRow.attName, filePath: this.currentRow.attPath }, true)const reader = new FileReader()reader.readAsText(res, 'UTF-8')reader.onload = e => {this.$refs.txt.innerText = e.target.result}})}},immediate: true}}
}
</script><style lang="scss" scoped>
.container {width: 100%;height: 100%;.txt {width: 100%;height: 100%;overflow: auto;word-break: break-all;border: 0;}.file {width: 100%;height: 100%;overflow: auto;border: 0;}.file ::v-deep .docx-wrapper {height: 100%;padding: 20px 20px;background-color: #fff;border: 1px solid #eee;}.file ::v-deep .docx-wrapper > section.docx {width: 100% !important;height: 80%;min-height: auto !important;padding: 0 !important;margin-bottom: 0;overflow-y: scroll;box-shadow: none;}.file ::v-deep .docx-wrapper > section.docx::-webkit-scrollbar {display: none;}.empty-content {width: 100%;height: 100%;overflow: hidden;text-align: center;border: 1px solid #eee;p {position: relative;top: 40%;font-size: 16px;color: #666;}}
}
</style>

对于文本文件txt,我们需要下载下来,然后读取里面的内容
,最好通过innerText插入div回显出来。


下面是文件下载的接口(这个需要后端提供):
// 文件下载
export function downloadFile(data) {return request({url: '/common/downloadFile',method: 'post',responseType: 'blob',data})
}
// 文件下载
export function downloadFileNew(data, needBlob = false) {return request({url: '/common/downloadFileNew',meta: { needBlob },method: 'post',responseType: 'blob',data})
}

http://www.ppmy.cn/server/167369.html

相关文章

微信小程序日程预约

涉及仪器的预约使用&#xff0c;仿照小米日历日程预约开发开发对应页。 效果展示 文章目录 效果展示需求分析代码实现一、构建基础页面结构1. 顶部日期选择器2. 中部canvas绘制3. 底部数据回显 二、中间canvas功能细分1. 激活状态的判断2. 时间块拉伸逻辑3. 时间块拖动逻辑 三…

如何使用deepseek等AI工具辅助web后端工作的开发

使用DeepSeek等AI工具辅助Web后端开发可以显著提升效率,以下是具体应用场景和操作指南: 一、核心开发场景 代码生成与补全示例场景:快速生成CRUD接口 操作:输入提示词 用Node.js Express框架编写用户管理模块,要求: - RESTful API设计 - 包含创建/查询/更新/删除接口 - …

微服务..

Spring Cloud 1. 什么是Spring Cloud&#xff1f; 答案&#xff1a; Spring Cloud是一个基于Spring Boot的开源框架&#xff0c;用于构建分布式系统。它提供了一系列的工具和库&#xff0c;用于简化微服务架构的开发和部署。Spring Cloud的核心功能包括服务注册与发现、配置管…

常用HAL库

宏定义 #define LED1_PIN GPIO_PIN_12 //定义引脚别名 #define LED1_GPIO_PORT GPIOB // GPIO_PIN_12 GPIOB确定指定引脚 #define LED1_GPIO_CLK_ENABLE() __HAL_RCC_GPIOA_CLK_ENABLE() //定义方法别名 #define LED1(a) HAL_GPIO_WritePin(LED1_GPIO_PORT,LED1_P…

网络安全设备异构要求 网络安全设备硬件

导航目录&#xff1a; 一、网络的设备 1. 网络传输介质互联设备2. 物理层互联设备3. 数据链路层互联设备4. 网络层互联设备5. 应用层互联设备 二、网络的传输介质 1. 有线介质2. 无线介质 三、组建网络 一、网络的设备 1. 网络传输介质互联设备 网络传输介质互联设备包括…

declare和less

declare -x LESSCLOSE"/usr/bin/lesspipe %s %s" declare -x LESSOPEN"| /usr/bin/lesspipe %s" declare 是一个在 **Unix/Linux Shell**&#xff08;如 Bash&#xff09;中用于声明变量及其属性的命令。它通常用于设置变量的值、类型以及一些特殊属性&a…

从MySQL优化到脑力健康:技术人与效率的双重提升

文章目录 零&#xff1a;前言一&#xff1a;MySQL性能优化的核心知识点1. 索引优化的最佳实践实战案例&#xff1a; 2. 高并发事务的处理机制实战案例&#xff1a; 3. 查询性能调优实战案例&#xff1a; 4. 缓存与连接池的优化实战案例&#xff1a; 二&#xff1a;技术工作者的…

常见的排序算法:插入排序、选择排序、冒泡排序、快速排序

1、插入排序 步骤&#xff1a; 1.从第一个元素开始&#xff0c;该元素可以认为已经被排序 2.取下一个元素tem&#xff0c;从已排序的元素序列从后往前扫描 3.如果该元素大于tem&#xff0c;则将该元素移到下一位 4.重复步骤3&#xff0c;直到找到已排序元素中小于等于tem的元素…