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

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

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

相关文章:

  • 【安当产品应用案例100集】037-强化OpenVPN安全防线的卓越之选——安当ASP身份认证系统
  • 【人工智能】解码语言之谜:使用Python构建神经机器翻译系统
  • 图像锐化(QT)
  • 棱光PDF工具箱:一站式解决你的各种需要
  • 使用Docker + Ollama在Ubuntu中部署deepseek
  • ASP.NET Core JWT
  • Android图片加载框架Coil,Kotlin
  • 国税局发票查验中英文验证码识别,识别率99.99%
  • 十四. Redis 新功能
  • day10-字符串
  • Spring Boot 与 Kafka 实现高效消息队列通信的最佳实践
  • flutter ListView Item复用源码解析
  • 亲自测试并推荐的第三方平台使用DeepSeek 和 LM Studio 本地部署DeepSeek
  • AI刷题-子数组和的最大值问题
  • 【Java 面试 八股文】Redis篇
  • 数字电路-基础逻辑门实验
  • Day 32 卡玛笔记
  • 基于 GEE 的网格化降雨数据可视化与时间序列分析
  • DeepSeek与AI提示语设计的全面指南
  • 【信息系统项目管理师-案例真题】2018上半年案例分析答案和详解
  • DeepSeek本地部署(解决ollama无法安装问题)
  • 【Java基础篇】——第4篇:Java常用类库与工具类
  • 深度学习-108-大语言模型LLM之基于langchain的结构化输出功能提取结构化信息
  • at coder ABC 392
  • Apache Kafka 消息清理之道
  • 【大数据安全分析】为什么要用大数据技术进行安全分析?