vue预览doc,docx,xlsx,pdf等文件方法

1.用微软的链接:不好用,文件地址得是公网的
var officrUrl = https://view.officeapps.live.com/op/view.aspx?src=文档绝对路径

2.和微软的相似,文件地址可以是非公网
var officrUrl=http://view.xdocin.com/xdoc?_xdoc=文件绝对路径

以上两个方法都可以直接预览各种类型的文件,放在iframe里面就可以了,方式为:

3.以上都不可以的话,只能分开预览了,
   1.doc我还没搞出来,docx--预览的话,可以下载docx-view插件,可以去看docx预览icon-default.png?t=M85Bhttps://mp.csdn.net/mp_blog/creation/editor/127528559
  2.pdf -- 可以直接将绝对路径放入iframe
  3.txt:先根据文件路径转为blob文件流

  然后放在input中就可以了

Vue预览docxdocpdf、xls、xlsx文件,可以使用第三方库来实现。 对于docxdoc、xls、xlsx文件,可以使用`js-xlsx`库来进行解析预览。该库可以将Excel文件转换为JSON格式,而对于Word文件,可以使用`docx.js`库来进行解析预览。 对于pdf文件,可以使用`pdf.js`库来进行预览。该库可以通过Canvas将PDF文件渲染成图片,然后在Vue中显示。 下面是一个示例代码,演示如何使用上述库来预览不同类型的文件: ```html <template> <div> <div v-if="fileType === 'docx' || fileType === 'doc'"> <div v-html="docContent"></div> </div> <div v-else-if="fileType === 'xls' || fileType === 'xlsx'"> <table> <tr v-for="row in excelData"> <td v-for="cell in row">{{ cell }}</td> </tr> </table> </div> <div v-else-if="fileType === 'pdf'"> <canvas ref="pdfCanvas"></canvas> </div> </div> </template> <script> import XLSX from 'xlsx'; import Docx from 'docx'; import pdfjsLib from 'pdfjs-dist'; export default { data() { return { fileType: '', docContent: '', excelData: [], }; }, mounted() { // 根据文件类型进行解析预览 if (this.fileType === 'docx' || this.fileType === 'doc') { const reader = new FileReader(); reader.onload = (event) => { const content = event.target.result; const doc = new Docx(); doc.load(content); this.docContent = doc.getHtml(); }; reader.readAsArrayBuffer(this.file); } else if (this.fileType === 'xls' || this.fileType === 'xlsx') { const reader = new FileReader(); reader.onload = (event) => { const content = event.target.result; const workbook = XLSX.read(content, { type: 'array' }); const sheetName = workbook.SheetNames[0]; const worksheet = workbook.Sheets[sheetName]; this.excelData = XLSX.utils.sheet_to_json(worksheet, { header: 1 }); }; reader.readAsArrayBuffer(this.file); } else if (this.fileType === 'pdf') { const canvas = this.$refs.pdfCanvas; const ctx = canvas.getContext('2d'); pdfjsLib.getDocument(this.file).promise.then((pdf) => { pdf.getPage(1).then((page) => { const viewport = page.getViewport({ scale: 1.5 }); canvas.height = viewport.height; canvas.width = viewport.width; page.render({ canvasContext: ctx, viewport, }); }); }); } }, props: { file: { type: File, required: true, }, }, created() { // 根据文件后缀名判断文件类型 const fileExtension = this.file.name.split('.').pop(); if (fileExtension === 'docx' || fileExtension === 'doc') { this.fileType = 'docx'; } else if (fileExtension === 'xls' || fileExtension === 'xlsx') { this.fileType = 'xls'; } else if (fileExtension === 'pdf') { this.fileType = 'pdf'; } }, }; </script> ``` 在上述代码中,根据文件后缀名判断文件类型,然后使用不同的库进行解析预览。对于Word文件,使用`docx.js`库将文件解析为HTML格式,然后在Vue中显示;对于Excel文件,使用`js-xlsx`库将文件解析为JSON格式,然后在Vue中生成表格;对于PDF文件,使用`pdf.js`库将文件渲染成图片,然后在Vue中显示。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值