vue项目实现前端预览word和pdf格式文件

最近做vue项目遇到一个需求,就是前端实现上传word或pdf文件后,后端返回文件对应的文件流,前端需要在页面上展示出来。word预览简单一些,pdf预览我试过pdfjs,vue-pdf总是报各种奇奇怪怪的bug,但最终总算解决了问题,先看一下页面最终呈现效果吧:

页面上传pdf文件效果如下:

在这里插入图片描述

页面预览pdf文件效果如下:

在这里插入图片描述

页面上传word文件效果如下:

在这里插入图片描述

页面预览word文件效果如下:

在这里插入图片描述
这里先从上传组件页面说起,上传页面组件完整代码如下,按钮里面v-show=“$checkPermission([‘Register_tuotpUpload’])“都是给这个按钮设置了按钮权限的,我们只需要关注上传那一部分的代码即可,我们用了el-u

  • 0
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue预览 Word 文件和 Excel 文件,可以通过引入第三方插件实现。 1. 首先,需要安装并引入 `office-ui-fabric-js` 和 `office-ui-fabric-react` 这两个依赖库。 ```bash npm install office-ui-fabric-js office-ui-fabric-react --save ``` 2. 然后,可以通过 `office-ui-fabric-react` 提供的 `DocumentCard` 和 `DocumentCardPreview` 组件来实现 Word 和 Excel 文件的预览。需要注意的是,这两个组件需要在 `componentDidMount` 生命周期中加载。 ```vue <template> <div> <DocumentCard> <DocumentCardPreview previewImages={[ { previewIconProps: { iconName: 'WordDocument' }, previewProps: { previewFileType: PreviewFileType.Document, previewFileUrl: 'http://example.com/word.docx' } } ]} /> </DocumentCard> <DocumentCard> <DocumentCardPreview previewImages={[ { previewIconProps: { iconName: 'ExcelWorkbook' }, previewProps: { previewFileType: PreviewFileType.Document, previewFileUrl: 'http://example.com/excel.xlsx' } } ]} /> </DocumentCard> </div> </template> <script> import { DocumentCard, DocumentCardPreview, PreviewFileType } from 'office-ui-fabric-react' export default { name: 'FilePreview', mounted () { require('office-ui-fabric-js/dist/css/fabric.min.css') require('office-ui-fabric-js/dist/css/fabric.components.min.css') }, components: { DocumentCard, DocumentCardPreview } } </script> <style scoped> /* 样式可以根据需求自行修改 */ </style> ``` 3. 最后,需要在后端将 Word 和 Excel 文件换为 PDF 格式,然后在前端通过 `iframe` 标签来展示 PDF 文件。 ```vue <template> <div> <iframe :src="pdfUrl" frameborder="0" width="100%" height="600"></iframe> </div> </template> <script> export default { name: 'FilePreview', data () { return { pdfUrl: '' } }, mounted () { this.getPdfUrl() }, methods: { getPdfUrl () { // 从后端获取 PDF 文件的 URL // ... this.pdfUrl = 'http://example.com/file.pdf' } } } </script> <style scoped> /* 样式可以根据需求自行修改 */ </style> ``` 以上是两种实现 Word 和 Excel 文件预览的方法,可以根据自己的需求选择合适的方式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值