验证上传的 Word 模板 Javascript

Validate Uploaded Word Template Javascript

我正在寻找一个 npm 包或一种在浏览器中访问上传的 word .docx 文件客户端的方法。我想验证用户的 .docx 文件,该文件将与变量数组(动态创建的)进行比较,以确认 .docx 文件包含所有正在使用的变量。

首选方法是上传.docx,访问docx客户端的内容,在客户端搜索变量(模板变量的格式为mustache "{{ }}"),然后提交表单到服务器。

示例与此类似。


WORD DOCX

“我叫{{ name }},我喜欢{{ sport }}。我也喜欢享受{{ food }}和一杯{{ drink }}。”


const templateVariables = ["name","sport","food","beer"];

通知*您上传的模板没有使用变量“啤酒”。

通知*您上传的模板包含一个额外的变量“drink”。


主要 objective 是能够通过客户端访问表单中上传的 .docx 的正文内容。我有适当的比较代码来处理那部分,我只是无法让文件的内容也有一些东西可以比较。

如果我的理解是正确的,您需要代码来读取客户端中的 docx 文件内容? 在这里。

<html>
  <head>
    <meta charset="utf-8" />
    <title>title</title>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/docxtemplater/3.19.5/docxtemplater.js"></script>
    <script src="https://unpkg.com/pizzip@3.0.6/dist/pizzip.js"></script>
  </head>
  <body>
    <input type="file"></input>
    <script>
      const input = document.querySelector("input[type=file]")
      input.addEventListener("change", () => {
        const reader = new FileReader();
        reader.onload = (e) => {
          const content = e.target.result;
          const zip = new PizZip(content);
          const doc = new docxtemplater().loadZip(zip)
          const text = doc.getFullText();
          alert(text);
        };
        reader.readAsBinaryString(input.files.item(0));
      });
    </script>
  </body>
</html>