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

Vue 简版文件预览笔记

简版文件预览笔记

调用方法

<script lang="ts" setup>import {exportFileData,preViewFile,} from '@/xxx/tools.ts';import {fileDownload,} from '@/api/xxx/xx';// 预览方法const handleViewBtn = () => {const filePath = 获取预览地址;const urlFormat = 获取预览地址的扩展名(xxx.toLowerCase()// 这里是预览接口请求,具体根据自己的接口来定fileDownload({ filePath }).then((res: any) => {// 导出的预览数据和预览类型let {filePathData , viewType} = exportFileData(res.data, urlFormat);switch (viewType) {case 'img':const imgObj: any = preViewFile('', '_blank', 1000, 700);imgObj.document.write(`<!DOCTYPE html><html><body><img src='${filePathData}' style='height:100%;width:100%'/></body></html>`);break;case 'txt':const txtObj: any = preViewFile('', '_blank', 1000, 700);txtObj.document.write(`<iframe src="${filePathData}" width="100%" height="100%" frameborder="0"> </iframe>`);break;case 'pdf_ppt_doc_xls':preViewFile(filePathData, '_blank', 1000, 700);break;default:console.log('没匹配到');break;}});};
</script>

公共方法

/*** @description: 预览文档时新打开窗口的样式* @param {string} url 预览地址* @param {string} name 名称* @param {number} w 宽度* @param {number} h 高度* @param {string} type 打开的类型  是pdf  还是图片  txt* @return {*}*/
export const preViewFile = (url: string, name: string, w: number, h: number) => {const iTop = (window.screen.availHeight - 30 - h) / 2;// const iLeft = (window.screen.availWidth - 10 - w) / 2;const iLeft = (window.screenX || window.screenLeft || 0) + (screen.width - w) / 2;const newwindow: any = window.open(url,name,`height=${h},innerHeight=${h},width=${w},innerWidth=${w}, top=${iTop},left=${iLeft},titlebar=no, directions=no, toolbar=no, menubar=no, scrollbars=yes, resizeable=no, location=no, status=no`);return newwindow;
};/*** @description: 二进制流对应的预览格式* @param {string} data 二进制流数据* @param {string} fileType 文件类型* @return {*}*/
export const exportFileData = (data: any, fileType: string) => {const formatList = fileFormat();let type = '';let fileURL: any = '';let viewType = ''for (const key in formatList) {if (key === fileType) {type = key;}}if (['png', 'jpeg', 'gif', 'bmp', 'jfif', 'jpg'].includes(type)) {fileURL = window.URL.createObjectURL(data); // 将查出的数据转换为图片路径viewType = 'img'} else if (['txt'].includes(type)) {const blob = new Blob([data], {type: `${formatList[type]}`,});fileURL = window.URL.createObjectURL(blob);viewType = 'txt'} else if (['xls', 'xlsx', 'doc', 'docx', 'pdf', 'pptx'].includes(type)) {const blob = new Blob([data], {type: 'application/pdf;chartset=UTF-8',});fileURL = window.URL.createObjectURL(blob);viewType = 'pdf_ppt_doc_xls'} else {fileURL = '';}return {fileURL,viewType};
};/*** @description: 文档下载根据上传类型 对应下载文档的后缀*/
export const fileFormat = () => {return {xls: 'application/vnd.ms-excel',xlsx: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',doc: 'application/msword',docx: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',pdf: 'application/pdf',pptx: 'application/vnd.openxmlformats-officedocument.presentationml.presentation',png: 'image/png',gif: 'image/gif',jpeg: 'image/jpeg',jpg: 'image/jpeg',bmp: 'image/bmp',tif: 'tif',tiff: 'image/tiff',txt: 'text/plain',jfif: 'jfif',};
};
http://www.lryc.cn/news/117488.html

相关文章:

  • 数据结构--栈和队列
  • 泰国的区块链和NFT市场调研
  • 精彩回顾 | D-Day深圳 上海站:高频策略研发再提速
  • 新法!《个人信息保护合规审计管理办法(征求意见稿)》解读
  • 南大通用数据库-Gbase-8a-学习-37-delete误删数据恢复方法
  • 【高光谱图像的去噪算法】通过全变异最小化对受激拉曼光谱图像进行去噪研究(Matlab代码实现)
  • UEditorPlus v3.3.0 图片上传压缩重构,UI优化,升级基础组件
  • 百度翻译API整合SpringBoot
  • Spring @Primary、@Order、JSR @Priority作用与区别
  • 【Mac】mac 系统下格式化U盘或移动硬盘为ext4格式
  • ubuntu20.4 sgx环境配置
  • 01.图片下拉触底分页加载每张图片
  • “精准学习嵌入式开发:明确目标,提升技能“
  • C语言--联合体-共用体
  • echarts实现中国地图下钻进入下一级行政区(地图钻取)
  • 从0到1学会手写操作系统,我只用了2个小时
  • 软件包管理
  • 【逗老师的PMP学习笔记】9、项目资源管理
  • react-virtualized可视化区域渲染的使用
  • navicat连接postgresql报错
  • 题目:灾后重建
  • Vue 插槽 slot
  • 【C/C++】C语言位图操作实例(亲测)
  • Mahout教程_编程入门自学教程_菜鸟教程-免费教程分享
  • wxwidgets Ribbon使用wxRibbonToolBar实例
  • 8.9黄金最新行情走势分析及短线交易策略
  • VB+SQL房地产评估系统设计与实现
  • 用AOP实现前端传参时间的时区转化
  • mybatis There is no getter for property named ‘*‘ in ‘class java.lang.String
  • Mac终端前总会出现 (base) 字样解决