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>
相关推荐
我是小路路呀1 天前
element级联选择器:已选中一个二级节点,随后又点击了一个一级节点(仅浏览,未确认选择),此时下拉框失去焦点并关闭
javascript·vue.js·elementui
程序员爱钓鱼1 天前
Node.js 编程实战:文件读写操作
前端·后端·node.js
PineappleCoder1 天前
工程化必备!SVG 雪碧图的最佳实践:ID 引用 + 缓存友好,无需手动算坐标
前端·性能优化
JIngJaneIL1 天前
基于springboot + vue古城景区管理系统(源码+数据库+文档)
java·开发语言·前端·数据库·vue.js·spring boot·后端
敲敲了个代码1 天前
隐式类型转换:哈基米 == 猫 ? true :false
开发语言·前端·javascript·学习·面试·web
澄江静如练_1 天前
列表渲染(v-for)
前端·javascript·vue.js
JustHappy1 天前
「chrome extensions🛠️」我写了一个超级简单的浏览器插件Vue开发模板
前端·javascript·github
Loo国昌1 天前
Vue 3 前端工程化:架构、核心原理与生产实践
前端·vue.js·架构
sg_knight1 天前
拥抱未来:ECMAScript Modules (ESM) 深度解析
开发语言·前端·javascript·vue·ecmascript·web·esm