vue纯前端实现文件预览(pdf,docx,png)超简单

项目种我们可能会用到文件预览这个功能

一般的文件预览这些前端就可以直接实现

pdf预览插件(vue-pdf)

安装

npm install --save vue-pdf

使用

 import pdf from “vue-pdf” 

在需要用的组件上直接引入

 

docx预览插件(mammoth)

该插件主要是把源文档转成了html,不支持doc文件

安装

npm install --save mammoth

 使用方法同上

import mammoth from ‘mammoth’

我简单做了一个支持图片,pdf,docx文件的在线预览,其他的后续可能会再加

下面就是我的组件的代码了

<template>
    <div>
        <div v-if="filetype == 1">
            <el-button-group>
                <el-button type="primary" icon="el-icon-arrow-left" size="mini" @click="prePage">上一页</el-button>
                <el-button type="primary" size="mini" @click="nextPage">下一页<i
                        class="el-icon-arrow-right el-icon--right"></i></el-button>
            </el-button-group>
            <div style="marginTop: 10px; color: #409EFF">{{ pageNum }} / {{ pageTotalNum }}</div>
            <pdf :page="pageNum" :src="url" @progress="loadedRatio = $event" @num-pages="pageTotalNum = $event"></pdf>
        </div>
        <div v-else-if="filetype == 2">
            <div v-html="docHtml"></div>
        </div>
        <div v-else-if="filetype == 3">
            <img :src="url" alt="" style="width: 500px; height: 500px;">
        </div>
    </div>
</template>
<script>
import pdf from 'vue-pdf'
import mammoth from 'mammoth'
export default {
    name: 'Pdf',
    components: {
        pdf,
    },
    props: {
        fileUrl: {
            type: String,
            default: '',
        }
    },
    data() {
        return {
            url: '',
            pageNum: 1,
            pageTotalNum: 1,// # 总页数
            loadedRatio: 0,// # 当前页面的加载进度,范围是0-1 ,等于1的时候代表当前页已经完全加载完成了
            filetype: '',
            docHtml:'',//docx文件展示
        }

    },
    watch: {
        fileUrl(newval) {
            console.log(newval);
            this.getFiletype(newval)
        }
    },
    created() {
        this.getFiletype(this.fileUrl)
        console.log(this.fileUrl, '地址');
    },
    methods: {

        /**
  * docx文件预览
  */
        initDocPreview() {
            // 请求文件流
            try {
                let _this = this
                const xhr = new XMLHttpRequest();
                xhr.open('get', this.url, true);
                xhr.responseType = 'arraybuffer';
                xhr.onload = function () {
                    if (xhr.status === 200) {
                        mammoth.convertToHtml({ arrayBuffer: new Uint8Array(xhr.response) }).then(function (resultObject) {
                            _this.docHtml = resultObject.value
                        })
                    } else if (xhr.status === 404) {
                        _this.$error({ message: "文件不存在" })
                    } else {
                        _this.$error({ message: "读取服务器文档异常" })
                    }
                }
                xhr.onerror = function () {
                    _this.$error({ message: "访问服务器异常,请检查访问链接是否正常" })
                }
                xhr.send();
            } catch (e) {
                this.docHtml = '<h1 style="text-align: center">暂无内容</h1>'
            }
        },
        getFiletype(url) {
            this.url = url
            if (!url) {
                return null
            }
            let fileName = this.url.lastIndexOf(".");//取到文件名开始到最后一个点的长度
            let fileNameLength = this.url.length;//取到文件名长度
            let fileFormat = this.url.substring(fileName + 1, fileNameLength);//截
            console.log(fileFormat)
            if (fileFormat.toLowerCase() === "pdf") {
                this.filetype = 1
            } else if (fileFormat.toLowerCase() === "docx") {
                this.filetype = 2
                this.initDocPreview()
            } else if (fileFormat.toLowerCase() === "jpeg" || fileFormat.toLowerCase() === "png" || fileFormat.toLowerCase() === "jpg") {
                this.filetype = 3
            }
            console.log(this.filetype);

        },
        // 上一页
        prePage() {
            let page = this.pageNum
            page = page > 1 ? page - 1 : this.pageTotalNum
            this.pageNum = page
        },

        // 下一页
        nextPage() {
            let page = this.pageNum
            page = page < this.pageTotalNum ? page + 1 : 1
            this.pageNum = page
        }
    }
}
</script>

只需要传进来一个url链接就可以了,快cv拿走用吧

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

搬砖的小马

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值