vue3+ts 实现文件在线预览

news/2024/10/23 7:40:25/

一、背景

企业微信自建项目中要求PC端也能进行文件预览,但是企业微信提供的接口只能在移动端使用,通过查阅资料修改成贴合项目的方法。参考:https://blog.csdn.net/w_t_y_y/article/details/115767747
kkFileView官方文档

二、preview-file组件

1.三种方式:
①、Office Web 查看器 http://view.officeapps.live.com/op/view.aspx?src=encodeuricomponent(url)
②、XDOC文档预览云服务 只能免费使用几天,后续需要付费400/年/IP
https://view.xdocin.com/view?src=encodeURIComponent(url)
③、kkFileView http://127.0.0.1:8012/onlinePreview?url='+encodeURIComponent(Base64.encode(url))

2.在components文件中,创建preview-file.vue文件,代码如下:

<template><div><!--图片--><!--d-flex f-jc-c:display:flex; justify-content: center;--><div class="d-flex f-jc-c" v-if="fileMessage.imgShow"><img :src="fileMessage.downloadUrl" /></div><!--doc,excel--><div v-else-if="fileMessage.docShow || fileMessage.excelShow"><!--Office Web 查看器 http://view.officeapps.live.com/op/view.aspx?src= --><!-- XDOC文档预览云服务  只能免费使用几天,后续需要付费 https://view.xdocin.com/view?src=encodeURIComponent(fileMessage.downloadUrl)  --><!-- kkFileView  http://127.0.0.1:8012/onlinePreview?url='+encodeURIComponent(Base64.encode(url))  --><iframe class="child" frameborder="0":src="'https://view.xdocin.com/view?src=' + encodeURIComponent(fileMessage.downloadUrl)":style="{ width: fileStyle.width, height: fileStyle.height }"></iframe></div><!--音频--><div v-else-if="fileMessage.audioShow"><!-- loop="loop"  循环播放 --><audio id="myaudio" :src="fileMessage.downloadUrl" controls="controls" preload="preload">不支持audio标签</audio></div><!--视频--><div class="d-flex f-jc-c" v-else-if="fileMessage.videoShow"><video preload="auto" align="center" controls="true"><source :src="fileMessage.downloadUrl" type="video/mp4" /></video></div><!--其他不能预览的--><div v-else-if="fileMessage.otherShow"></div></div>
</template><script lang="ts" setup>import Video from 'video.js';import 'video.js/dist/video-js.css';interface Props {file: any}interface Emits {(event: 'downLoadFile', id: string | number): void
}interface FileMessage {fileId: string | number,fileName: string,downloadUrl: string,imgShow: boolean,docShow: boolean,excelShow: boolean,audioShow: boolean,videoShow: boolean,//其他不能预览的otherShow: boolean,}interface FileStyle {height: string,width: string
}const $props = defineProps<Props>();const $emits = defineEmits<Emits>();const file = computed(() => $props.file);// 文件信息
const fileMessage = reactive<FileMessage>({fileId: '',fileName: '',downloadUrl: '',imgShow: false,docShow: false,excelShow: false,audioShow: false,videoShow: false,//其他不能预览的otherShow: false,
})const fileStyle = reactive<FileStyle>({height: window.innerHeight + 'px',width: '100%'})// 判断文件类型
const judgeFileType = () => {fileMessage.fileName = file.value.fileName;fileMessage.downloadUrl = file.value.downloadUrl;fileMessage.fileId = file.value.id;console.log(fileMessage.downloadUrl, 'fileMessage');if (fileMessage.fileName.endsWith('png') ||fileMessage.fileName.endsWith('jpg') ||fileMessage.fileName.endsWith('jpeg') ||fileMessage.fileName.endsWith('gif')) {//图片fileMessage.imgShow = true;} else if (fileMessage.fileName.endsWith('docx') ||fileMessage.fileName.endsWith('doc') ||fileMessage.fileName.endsWith('pdf') ||fileMessage.fileName.endsWith('pptx') ||fileMessage.fileName.endsWith('ppt') ||fileMessage.fileName.endsWith('txt')) {//docfileMessage.docShow = true;} else if (fileMessage.fileName.endsWith('xlsx') || fileMessage.fileName.endsWith('xls')) {//excelfileMessage.excelShow = true;}else if (fileMessage.fileName.endsWith('mp3')) {fileMessage.audioShow = true;}else if (fileMessage.fileName.endsWith('mp4')) {fileMessage.videoShow = true;} else {// fileMessage.otherShow = true;$emits('downLoadFile', fileMessage.fileId);}}onMounted(async () => {await judgeFileType();})</script><style scoped lang="scss">
img {width: 50%;
}audio {width: 100%;
}video {width: 30%;
}.child {// width: 100%;// height: 100%;scrolling: no;frameborder: 0;border: 0;marginwidth: 0;marginheight: 0;
}
</style>

三、preview-file使用

1.template中使用方式

<!-- 查看附件 --><van-popup v-model:show="previewFile.fileShow"><preview-file v-if="previewFile.fileShow" :file="previewFile.fileMessage" @downLoadFile="downLoadFile"></preview-file></van-popup>

2.方法处理:

// 预览文件
const previewFile = reactive({fileShow: false,fileMessage: {id: '',fileName: '',downloadUrl: '',}
});// 下载附件 适配PC Iphone Android
const handleDownFile = (file: any) => {// 不是企业微信if (!isWxWork()) {return drNotify('请用企业微信打开');}isMobile() ?(isAndroid() ? window.open(handleImgSrc(file.group, file.filepath)) : handlePreviewFile(file)):PCpreview(file);
}// pc端预览文件
const PCpreview = (file: any) => {previewFile.fileMessage.id = file.id;previewFile.fileMessage.fileName = file.filename;// handleImgSrc --- 为下载的路径previewFile.fileMessage.downloadUrl = handleImgSrc(file.group, file.filepath);previewFile.fileShow = true;}// 下载附件  PC端
const downLoadFile = (id: string | number) => {previewFile.fileShow = false;downloadLogFile.derive({ id }).then((res: any) => {if (!res.message) { }})
}

PS:Iphone与Android区分开来的原因:使用企业微信提供的文件预览的功能,Android使用会自动跳转浏览器并下载。

效果图展示:
在这里插入图片描述
在这里插入图片描述


http://www.ppmy.cn/news/213797.html

相关文章

HTTP服务原理,做运维的你了解多少呢?

一、HTTP服务介绍 1 简述用户访网站流程 a 进行域名信息的DNS解析 dig trace 获得www.oldboyedu.com ip地址信息 b 进行与网站服务器建立连接&#xff0c;tcp三次握手过程 &#xff08;syn ack SEQ ACK/状态转换 closed listen syn_sent syn_rcvd established &#xff09;…

收费!ChatGPT官方版API来了

来源&#xff1a;SegmentFault思否 3 月 2 日&#xff0c;OpenAI 在官方博客宣布&#xff0c;开放 ChatGPT 和 Whisper 的模型 API &#xff0c;用户可将其集成在应用程序等产品中。ChatGPT API 价格为 1k tokens/$0.002&#xff0c;等于每输出 100 万个单词&#xff0c;价格才…

第04课:适配器模式

适配器模式将一个类的接口,转换成客户期望的另一个接口。适配器让原本接口不兼容的类可以合作无间 下面讲一件真事:本人去年年底公司年会抽奖抽中了一台粪叉(iphone x),由于没有现货,大概过了一个月左右,抽中的粪叉才送到我手中。拿到手一看,咦,是港版的。略微有些失望…

NX CAM 加工模块二次开发视频教程全集目录(胡君录制)

CAM加工模块开发-第1节&#xff1a;CAM开发前言、规划以及基础能力储备说明.mp4 CAM加工模块开发-第2节&#xff1a;CAM模块构架原理、模板、模块切换等.mp4 CAM加工模块开发-第3节&#xff1a;UFun帮助文档关于CAM加工的函数的分布情况.mp4 CAM加工模块开发-第4节&#xff1a;…

价值2400全栈开发Flask Python Web 网站编程

01-计算机基础常识.mp4 02-Python语言概览、安装与运行.mp4 03-Python 变量、数据类型及存储.mp4 04-Python 常用数据类型概览.mp4 05-数值与字符串.mp4 06-列表list.mp4 07-字典表dict 与元组 tuple.mp4 08-文件与类型汇总.mp4 09-语句、表达式与if分支.mp4 10-循环语…

C1-01-作业(植物大战僵尸修改二维码图片码)

1、用二进制0 1 绘制名字 1为白&#xff0c;0为黑。 图像文件存储的都是每一个像素对应的颜色值。 1、位图文件有两种存储像素数据的格式。16777216色&#xff08;真彩色&#xff09;的图像&#xff0c;一个像素的颜色可以用24位数据表示。256色的图像可以用调色板对颜色的信…

音视频开发第一课-使用C语言开发视频播放器 650元IT外包开发全程记录

界面设计 目标效果&#xff1a; 创建MFC对话框项目&#xff0c;或者直接使用项目模板 主要选择64位平台。 拖放控件 设置播放器区域的背景 把资源bg.bmp拷贝到项目目录的res目录下。 把bg.bmp添加当项目的资源中 把图片控件的类型修改为Bitmap, 并设置为刚添加的图片资源。…

电影版本格式知多少

电影格式相关知识介绍&#xff1a; 1.CAM&#xff08;枪版&#xff09;——珍爱生命&#xff0c;远离枪版 CAM 通常是用数码摄像机从电影院盗录。有时会使用小三角架&#xff0c;但大多数时候不可能使用&#xff0c;所以摄像机会抖动。因此我们看到画面通常偏暗人物常常会失真…