Angular中Mammoth转换docx报错Could not find file in options解决方案问询
错误修复方案
根因说明
你传入convertToHtml方法的配置对象键名不符合Mammoth的API规范,你使用了大写开头的ArrayBuffer作为参数键,而Mammoth要求接收ArrayBuffer数据的键名为小写开头的arrayBuffer,框架无法识别到你传入的文件数据,因此抛出找不到文件的错误。
修正后代码
convertDocument(event):void{ if (this.fileToUpload!=null){ fileToArrayBuffer(this.fileToUpload).then((data) => { console.log(data); // 仅修改参数键名即可 convertToHtml({ arrayBuffer : data}) .then(function(result){ var html = result.value; // 生成的HTML console.log(html); var messages = result.messages; // 转换过程中的警告等消息 }) .catch(err => console.error('转换异常:', err)) // 建议新增异常捕获逻辑 .done(); }) } }
补充注意事项
- 若你是从mammoth包直接导入方法,建议使用
import * as mammoth from 'mammoth'的方式导入,调用时写为mammoth.convertToHtml(),避免和项目内其他同名方法冲突 - 如有自定义样式需求,可以在配置对象中新增
styleMap参数,自定义docx样式和HTML标签的映射规则
内容的提问来源于stack exchange,提问作者lou habert
相关产品推荐
相关产品推荐

