前端预览doc文件

今天让做一个登录页面的用户协议和隐私协议的预览

首先在vue项目的public文件夹的static文件夹项目放入

方法一

使用mammoth.js   会忽略复杂样式 我的版本是  "mammoth": "^1.7.2"

<template>
  <div class="word-wrap-ss">
    <div id="wordView" v-html="wordText" />
  </div>
</template>

<script>
// docx文档预览(只能转换.docx文档,转换过程中复杂样式被忽,居中、首行缩进等)
import mammoth from 'mammoth'
export default {
  name: 'Word1000',
  data() {
    return {
      wordText: '',
      wordURL: 'http://192.168.2.50:8080/static/userProtocol.docx' //文件地址
    }
  },
  created() {
    let str = window.location.href
    let arr = str.split('#')
    this.wordURL = arr[0] + 'static/userProtocol.docx'
    this.getWordText()
  },
  methods: {
    getWordText() {
      const xhr = new XMLHttpRequest()
      xhr.open('get', this.wordURL, true)
      xhr.responseType = 'arraybuffer'
      xhr.onload = () => {
        if (xhr.status == 200) {
          mammoth.convertToHtml({ arrayBuffer: new Uint8Array(xhr.response) }).then(resultObject => {
            this.$nextTick(() => {
              this.wordText = resultObject.value
            })
          })
        }
      }
      xhr.send()
    }
  }
}
</script>

<style>
.word-wrap-ss {
  padding: 20px;
  font-size: medium;
}
</style>

方法二

使用docx-preview  

这里特别需要注意版本,我使用的这两个版本才正常运行

"docx-preview": "^0.1.4",    "jszip": "^3.10.0",

window.JSZip = require('jszip')   这一步是必要的,看了原码发现docx-preview里面require('JSZip')

<template>
  <div class="word-wrap-ss">
    <div id="file" />
  </div>
</template>

<script>
const docx = require('docx-preview')
window.JSZip = require('jszip')
export default {
  name: 'Word1000',
  data() {
    return {
      wordURL: 'http://192.168.2.50:8080/static/userProtocol.docx' //文件地址
    }
  },
  created() {
    let str = window.location.href
    let arr = str.split('#')
    this.wordURL = arr[0] + 'static/userProtocol.docx'
    this.getWordText()
  },
  methods: {
    getWordText() {
      const xhr = new XMLHttpRequest()
      xhr.open('get', this.wordURL, true)
      xhr.responseType = 'blob'
      xhr.onload = () => {
        if (xhr.status == 200) {
          docx
            .renderAsync(xhr.response, document.getElementById('file'), null, {
              className: 'docx', // 默认和文档样式类的类名/前缀
              inWrapper: true, // 启用围绕文档内容渲染包装器
              ignoreWidth: false, // 禁止页面渲染宽度
              ignoreHeight: false, // 禁止页面渲染高度
              ignoreFonts: false, // 禁止字体渲染
              breakPages: false, // 在分页符上启用分页
              ignoreLastRenderedPageBreak: false, //禁用lastRenderedPageBreak元素的分页
              experimental: false, //启用实验性功能(制表符停止计算)
              trimXmlDeclaration: false, //如果为真,xml声明将在解析之前从xml文档中删除
              debug: false // 启用额外的日志记录
            })
            .then(res => {
              console.log('res---->', res)
            })
            .catch(err => {
              console.log('err---->', err)
            })
        }
      }
      xhr.send()
    }
  }
}
</script>

  • 5
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
由于doc文件是二进制格式,需要转换成HTML格式才能在前端进行预览。以下是一种可能的实现方式: 1. 安装依赖 使用`npm install mammoth`安装mammoth库,该库可以将docx和doc文件转换成HTML格式。 2. 上传文件 用户上传doc文件后,可以使用`FileReader`对象读取文件内容,并将其转换成二进制格式。 ```javascript const fileInput = document.getElementById('file-input'); fileInput.addEventListener('change', (event) => { const file = event.target.files[0]; const reader = new FileReader(); reader.readAsArrayBuffer(file); reader.onload = () => { const arrayBuffer = reader.result; // 将arrayBuffer发送到后端进行转换 } }) ``` 3. 后端转换 将二进制数据发送到后端,后端使用mammoth库将doc文件转换成HTML格式,并返回给前端。 ```javascript const mammoth = require('mammoth'); const express = require('express'); const app = express(); app.post('/convert', (req, res) => { const buffer = req.body; // 二进制数据 mammoth.convertToHtml({ arrayBuffer: buffer }) .then(result => { const html = result.value; res.send(html); }) .catch(error => { console.error(error); res.status(500).send('Error converting file'); }); }) ``` 4. 前端显示 前端接收到HTML格式的文件内容后,可以使用`innerHTML`属性将其显示在页面上。 ```javascript const previewDiv = document.getElementById('preview-div'); previewDiv.innerHTML = html; ``` 注意:由于doc文件可能包含JavaScript代码和外部资源,将其转换成HTML格式后存在一定的安全风险,需要进行安全性检查。另外,由于不同版本的Word软件可能生成的doc文件格式不同,转换成HTML格式的结果可能不完全一致,需要进行兼容性测试。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

代码老祖

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

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

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

打赏作者

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

抵扣说明:

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

余额充值