您的位置:首页 > 汽车 > 新车 > 幼儿园课程设计网络图_沭阳网站定制_网络营销的方法包括哪些_网站设计说明

幼儿园课程设计网络图_沭阳网站定制_网络营销的方法包括哪些_网站设计说明

2024/12/24 10:50:49 来源:https://blog.csdn.net/weixin_43581952/article/details/144609752  浏览:    关键词:幼儿园课程设计网络图_沭阳网站定制_网络营销的方法包括哪些_网站设计说明
幼儿园课程设计网络图_沭阳网站定制_网络营销的方法包括哪些_网站设计说明

实现附件在线预览是一个很常用的功能,这次正好碰到这样的需求,记录一下自己实现的过程。

首先是插件的选择,网上实现预览的方法主要有两种,一个是vue-office插件,另一个是docx-preivew插件。看网上其他网友的教程都能够实现文件的在线预览。

选择一:vue-office插件

vue-office官网地址:docx文件预览 | vue-office

不知道是不是因为自己vue版本太低不兼容这个插件,还是其他原因,按照方法怎么都实现不了文件的预览。(项目的vue版本是2.5.17,如果有遇到类似情况解决了的,欢迎指教)

选择二:docx-preivew插件

参考文章:docx-preview在vue内的使用_vue docx-preview-CSDN博客

第一种方式实现不了,就选择了第二种。第二个插件主要的问题就是版本的选择。一开始直接无脑操作根据命令(npm i docx-preview)安装插件:

结果出事了。我的vue版本过低,不能直接使用最新版本的docx-preivew插件。后面终于,在网上找到类似的问题,需要安装指定版本的插件,命令:

npm i docx-preview@0.1.20

有两种实现方法 :

一、上传文件进行预览

    <el-dialogtitle="文件预览":visible="dialogVisible"show-closeappend-to-bodywidth="60%":before-close="cancelDialog"><input type="file" @change="handleFileChange" /><div class="container" ref="container" id="container"></div><div slot="footer" class="dialog-footer tc"><button class="btn-submit btn-submit-sm" @click="cancelDialog()">关闭</button></div></el-dialog>
    async handleFileChange(e){const file = e.target.files[0];const blob = await this.fileToBlob(file);this.preview(blob,this.$refs.container,this.$refs.container);},fileToBlob(file) {return new Promise((resolve,) => {let reader = new FileReader();reader.readAsArrayBuffer(file);let blob = null;reader.onload = (e) => {if (typeof e.target.result === "object") {blob = new Blob([e.target.result]);} else {blob = e.target.result;}resolve(blob);};});},async preview(blob,createEl,styleEl) {await renderAsync(blob, // could be any type that supported by JSZip.loadAsynccreateEl, //element to render document content,styleEl, //element to render document styles, numbeings, fonts. If null, bodyContainer will be used.{className: "docx", //class name/prefix for default and document style classesinWrapper: true, //enables rendering of wrapper around document contentignoreWidth: false, //disables rendering width of pageignoreHeight: false, //disables rendering height of pageignoreFonts: false, //disables fonts renderingbreakPages: true, //enables page breaking on page breaksignoreLastRenderedPageBreak: true, //disables page breaking on lastRenderedPageBreak elementsexperimental: false, //enables experimental features (tab stops calculation)trimXmlDeclaration: true, //if true, xml declaration will be removed from xml documents before parsinguseBase64URL: false, //if true, images, fonts, etc. will be converted to base 64 URL, otherwise URL.createObjectURL is useduseMathMLPolyfill: false, //@deprecated includes MathML polyfills for chrome, edge, etc.renderChanges: false, //enables experimental rendering of document changes (inserions/deletions)renderHeaders: true, //enables headers renderingrenderFooters: true, //enables footers renderingrenderFootnotes: true, //enables footnotes renderingrenderEndnotes: true, //enables endnotes renderingdebug: false, //enables additional logging},);},

实现效果:

二、后端提供接口下载文件后预览 

    <el-button@click="toPreView($event, row)"type="text"class="pd-0 btn-table-wid2">预览</el-button><el-dialogtitle="文件预览":visible="dialogVisible"show-closeappend-to-bodywidth="60%":before-close="cancelDialog"><div class="container" ref="container" id="container"></div><div slot="footer" class="dialog-footer tc"><button class="btn-submit btn-submit-sm" @click="cancelDialog()">关闭</button></div></el-dialog>
  created() {this.previewFile(this.docId);},methods: {previewFile() {const inParam = {DOC_ID: this.docId,STAFF_NAME: this.$store.getters.staffName,SYS_USER_CODE: this.$store.getters.systemUserCode};this.$preview(this.mciApi.common.file.previewFile, {...inParam},false).then(r => {this.preview(r,this.$refs.container,this.$refs.container);})},async preview(blob,createEl,styleEl) {await renderAsync(blob, // could be any type that supported by JSZip.loadAsynccreateEl, //element to render document content,styleEl, //element to render document styles, numbeings, fonts. If null, bodyContainer will be used.{className: "docx", //class name/prefix for default and document style classesinWrapper: true, //enables rendering of wrapper around document contentignoreWidth: false, //disables rendering width of pageignoreHeight: false, //disables rendering height of pageignoreFonts: false, //disables fonts renderingbreakPages: true, //enables page breaking on page breaksignoreLastRenderedPageBreak: true, //disables page breaking on lastRenderedPageBreak elementsexperimental: false, //enables experimental features (tab stops calculation)trimXmlDeclaration: true, //if true, xml declaration will be removed from xml documents before parsinguseBase64URL: false, //if true, images, fonts, etc. will be converted to base 64 URL, otherwise URL.createObjectURL is useduseMathMLPolyfill: false, //@deprecated includes MathML polyfills for chrome, edge, etc.renderChanges: false, //enables experimental rendering of document changes (inserions/deletions)renderHeaders: true, //enables headers renderingrenderFooters: true, //enables footers renderingrenderFootnotes: true, //enables footnotes renderingrenderEndnotes: true, //enables endnotes renderingdebug: false, //enables additional logging},);},cancelDialog(){this.$emit('cancelDialog');},}

注意:

1、this.$preview 是封装的post请求方法,需要设置 responseType: 'blob'

2、this.mciApi.common.file.previewFile 是后台提供的文件下载接口,返回文件流数据,请求成功后将文件流转换成 blob 类型,进行文件预览。

3、blobconst blob = new Blob([content], { type: 'application/pdf' });

实现效果:

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com