如何在 Angular 10 中访问 .odt 文件OpenOffice中的文本

问题描述

我的目标是以编程方式在 .odt 文件中找到一些单词(由 input type="file" 选择)并使用 Angular 10 将它们替换为另一个。主要问题是我无法管理如何“解码”这个正确的文件类型,所以我可以访问它的内容。 我尝试了一些外部 js 库,如 mammoth.js,根据插入的文件生成 HTML,但它仅适用于 .docx 和 .doc 文件。我也试过 Blob() 和 FileReader() 的组合:

let reader = new FileReader();
reader.addEventListener("loadend",function() {
  const blob = new Blob([reader.result],{type: 'application/vnd.oasis.opendocument.text'});
  blob.text().then(res => console.log('blob res',res));
});
reader.readAsText(file);

但结果我得到了一些不可读的符号和问号,所以不可能找到一些东西。 我发现的另一个解决方案是一个名为 odt.js (https://github.com/codexa/odt.js) 的库,但我无法使用 npm 安装它,只能通过 html 中的“script”标签(如果在 Angular 中可能,请需要有关如何导入的建议)正确)。这是我的一些代码

html:

<input accept=".odt"type="file" (change)="onFileInput($event)" style="display:none;" [formControl]="companyTemplateAdd"/>
                            

.ts:

  onFileInput (event) {
    const file = event.target.files[0];
    this.currentCompanyTemplateName = file.name

    let reader = new FileReader();
    reader.addEventListener("loadend",function() {
      const blob = new Blob([reader.result],{type: 'application/vnd.oasis.opendocument.text'});
      blob.text().then(res => console.log('blob res',res));
    });
    reader.readAsText(file);
    
  }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)