[vue]mammoth将docx转为html

765 阅读1分钟

html部分

<button class="yellow-text ml10" @click="$refs.doc.click()">導入文件</span>
<input ref="doc" accept=".docx" style="position:absolute;left: 9999px;" type="file" @change="getWordFile">

js部分

import mammoth from "mammoth";
getWordFile(e) {
  if (e.target.files.length == 0) return
  const file = e.target.files[0]
  let reader = new FileReader()
  reader.readAsArrayBuffer(file)
  reader.onload = (evt) => {
    let arrayBuffer = evt.target.result
    mammoth
      .convertToHtml({arrayBuffer: arrayBuffer})
      .then(res => {
         //res.value 就是生成的HTML文件,可以直接赋值给富文本编辑器
      })
      .done()
  }
}