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

news/2025/2/12 1:04:12/

我们知道要想在前端预览文件,可以通过浏览器自带的预览功能,我们只要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/news/1571286.html

相关文章

【LeetCode 热题100】74:搜索二维矩阵(二分、线性两种方式 详细解析)(Go 语言实现)

&#x1f680; 力扣热题 74&#xff1a;搜索二维矩阵&#xff08;详细解析&#xff09; &#x1f4cc; 题目描述 力扣 74. 搜索二维矩阵 给你一个满足下述两条属性的 m x n 整数矩阵 matrix &#xff1a; 每行中的整数从左到右按非递减顺序排列。每行的第一个整数大于前一行的…

LSSVM最小二乘支持向量机多变量多步光伏功率预测(Matlab)

代码下载&#xff1a;LSSVM最小二乘支持向量机多变量多步光伏功率预测&#xff08;Matlab&#xff09; LSSVM最小二乘支持向量机多变量多步光伏功率预测 一、引言 1.1、研究背景与意义 随着全球能源危机和环境问题的日益严重&#xff0c;可再生能源的开发利用成为了世界各国…

Day62_补20250210_图论part6_108冗余连接|109.冗余连接II

Day62_20250210_图论part6_108冗余连接|109.冗余连接II 108冗余连接 【把题意转化为并查集问题】 题目 有一个图&#xff0c;它是一棵树&#xff0c;他是拥有 n 个节点&#xff08;节点编号1到n&#xff09;和 n - 1 条边的连通无环无向图&#xff08;其实就是一个线形图&am…

Vue 3 30天精进之旅:Day 16 - 组合式API进阶

友情提示&#xff1a;本文内容全部由 银河易创&#xff08;https://ai.eaigx.com&#xff09;AI创作平台生成&#xff0c;仅供参考。请根据具体情况和需求进行适当的调整和验证。 欢迎来到“Vue 3 30天精进之旅”的第16天&#xff01;今天我们将深入探讨 组合式API 的进阶用法&…

如何在WPS和Word/Excel中直接使用DeepSeek功能

以下是将DeepSeek功能集成到WPS中的详细步骤&#xff0c;无需本地部署模型&#xff0c;直接通过官网连接使用&#xff1a;1. 下载并安装OfficeAI插件 &#xff08;1&#xff09;访问OfficeAI插件下载地址&#xff1a;OfficeAI助手 - 免费办公智能AI助手, AI写作&#xff0c;下载…

matlab simulink 船舶模糊pid控制仿真

1、内容简介 略 matlab simulink 118-船舶模糊pid控制仿真 可以交流、咨询、答疑 2、内容说明 略 3、仿真分析 略 4、参考论文 略 基于船舶运动控制的Matlab仿真.pdf

Linux系统编程之信号基础知识

概述 信号是Linux系统中用于进程间通信的一种机制&#xff0c;允许一个进程通知另一个进程发生了某些特定事件。信号可以来自硬件中断、用户输入&#xff0c;也可以来自其他进程或者内核本身。信号是一种异步通知机制&#xff0c;当某个事件发生时&#xff0c;操作系统会向目标…

【小鱼闪闪】自制物联网测温小程序远程监测温度(图文)

小飞鱼之前写过一个将测温元件读取数据后写入到本地服务器的程序&#xff0c;可是这样有一个缺点就是只能局限于局域网来查询数据&#xff0c;而对于在办公楼外就不能查看数据。为了解决这个问题&#xff0c;今天小飞鱼做了一个可以将温度数据写入到云端的程序&#xff0c;再通…