js 前端页面页面展示docx文件【插件docx-preview】

需求: 页面展示docx文件

使用插件:docx-preview.min.js,以及该插件依赖jszip.min.js

1、jszip.min.js 地址:https://cdn.bootcdn.net/ajax/libs/jszip/3.10.0/jszip.min.js

2、docx-preview.min.js 地址: https://github.com/VolodymyrBaydalka/docxjs 

//自测项目用了vue
3、vue.js地址:https://cdn.jsdelivr.net/npm/vue/dist/vue.js

插件使用实例

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
	<div id="app">
	   <input type="file" @change="upload">
	   <div ref="file"></div>
	</div>
	<script src="./jszip.min.js"></script>
	<script src="./vue.js"></script>
	<script src="./docx-preview.min.js" type="text/javascript"></script>
<script>
    var app = new Vue({
        el: '#app',
        data: {
            file: null
        },
		mounted(){
			this.getApi("./****.docx");//本地文档地址:./****.docx
		},
        methods: {
        	//1、通过input预加载展示文档
            upload(e) {
                this.file = e.target.files[0]
				console.log('this.file=-======',this.file)
                docx.renderAsync(this.file, this.$refs.file);
            },
            //2、直接请求本地文档
			getApi(path){
				let _that = this;
				let xhr = new XMLHttpRequest();
				xhr.open('GET', path, true);
				xhr.responseType = 'blob';
				xhr.onload = function (e) {
				  if (this.status == 200) {
				  	//docx文档blob对象type类型值为:
				  	//application/vnd.openxmlformats-officedocument.wordprocessingml.document
				    let blob = new Blob([this.response], {type: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'});
				   
				    // 使用blob对象进行操作
						console.log('blob=====',blob)
						docx.renderAsync(blob, _that.$refs.file);
				  }
				};
				xhr.send();
			}
        }
    })
</script>
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值