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-docx
            v-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 FileCenterModel
            currentAttachment.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 = true
    infoLookFile.value = JSON.stringify(row)
}
const CloselookFile = () => {
    lookFileVisible.value = false
    infoLookFile.value = undefined
}
</script>
相关推荐
fruge几秒前
Vue 3 完全指南:响应式原理、组合式 API 与实战优化
javascript·vue.js·ecmascript
用户877244753962 分钟前
Lubanno7UniverSheet:开放底层能力,让你的表格需求 “不设限”
前端
张可爱8 分钟前
ES6奶茶铺版通俗笔记 🍵✨
前端
用户877244753968 分钟前
Lubanno7UniverSheet:选择命令式,为了真正的跨框架通用
前端
paopaokaka_luck8 分钟前
基于SpringBoot+Vue的数码交流管理系统(AI问答、协同过滤算法、websocket实时聊天、Echarts图形化分析)
vue.js·人工智能·spring boot·websocket·echarts
Aoda14 分钟前
从痛点到落地:PawHaven 的 Monorepo 架构设计
前端·javascript
望获linux17 分钟前
【实时Linux实战系列】使用 u-trace 或 a-trace 进行用户态应用剖析
java·linux·前端·网络·数据库·elasticsearch·操作系统
zxg_神说要有光24 分钟前
我好像找到了最适合我的生活状态
前端·javascript
飞哥数智坊29 分钟前
今天,我的个人网站正式上线了!
前端
念念不忘 必有回响1 小时前
前端自动化部署全流程(Jenkins + Nginx)
前端·自动化·jenkins