vue 预览并下载本地word
需要下载 docx-preview
<template>
<div class="docxPreview">
<el-row class="el-row-down">
<el-button @click="downLoadFile" size="small">导出</el-button>
</el-row>
<main>
<div ref="file" style="width: 100%; height: 100%"></div>
</main>
</div>
</template>
<script>
const docx = require("docx-preview");
import Axios from "axios";
import _ from "lodash";
export default {
components: {},
// 定义属性
data() {
return {};
},
// 计算属性,会监听依赖属性值随之变化
computed: {},
// 监控data中的数据变化
watch: {},
created() {},
mounted() {
this.getPdfCode();
},
// 方法集合
methods: {
getPdfCode() {
Axios({
method: "get",
responseType: "blob",
url: "/test1.docx",
}).then((data) => {
console.log(data);
docx.renderAsync(data, this.$refs.file); // 渲染到页面
});
},
downLoadFile: _.debounce(function () {
window.open("/test1.docx", ["_self"]);
}, 250),
},
};
</script>
<style lang="scss" scoped>
.docxPreview {
display: flex;
flex-direction: column;
height: 100%;
width: 100%;
.el-row-down {
background-color: #808080;
display: flex;
justify-content: end;
padding: 10px 20px;
}
main {
flex: 1;
background-color: #fff;
display: flex;
flex-direction: column;
height: 100%;
overflow: auto;
}
}
</style>
页面效果