docx文件新窗口预览

需要借助docx-preview。doc文件暂时没有做出预览,用了好多工具都不行。

直接npm i docx-preview 然后在文件里引入就行了

// html 
<el-dialog v-model="wordViewShow" fullscreen title="Tips" width="90%" class="previewWrap">
        <div ref="bottom">
            <div class="my-component" ref="wordView" id="wordView"></div>
        </div>
    </el-dialog>
// js逻辑
// 定义
import { renderAsync } from "docx-preview";
const wordViewShow = ref(false)
// 逻辑
(这里做判断是因为还有pdf的预览)  url是后端返回的文件地址
const extName = url.slice(url.lastIndexOf('.'))
if (extName === '.docx') {
            axios({
                method: 'get',
                responseType: 'blob', // 设置响应文件格式
                url: 'http://localhost:8888' + url, // 前面加自己网址的前缀,如果是完整路径可以不加
            }).then(({ data }) => {
                // 在新标签页中打开 Blob 数据
                const newWindow = window.open();
                newWindow.document.write('<html><head><title>docx文件</title></head><body>');
                newWindow.document.write('<div id="preview-container"></div>'); // 在新页面中创建一个容器用于显示 Blob 数据
                newWindow.document.write('</body></html>');
                newWindow.document.close();
                // 将 Blob 数据渲染到新页面中
                const previewContainer = newWindow.document.getElementById('preview-container');
                renderAsync(data, previewContainer);
                // renderAsync(data, proxy.$refs.wordView) // 渲染到页面预览(本页面预览)
            })
        }

展示效果(打印可以用浏览器自带的)

  • 7
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值