[英]Why am I getting a syntax error when I try to take info from this JSON file?
[英]Why am I getting no such file error when using "mammoth" to convert a simple .docx file?
我的代碼如下。 該文件的內容是一個簡單的“hello world”,我在調用這個龐大函數的同一個文件夾中有 hello.docx 文件。
錯誤結果:致命錯誤:ENOENT:沒有這樣的文件或目錄,打開'./hello.docx'
知道我做錯了什么嗎? 我在我的快速節點路由中使用它
mammoth.extractRawText({path: "./hello.docx"}) .then(function(result){ var text = result.value; // The raw text console.log(text); // var messages = result.messages; }) .done();
顯示的錯誤似乎很清楚無法讀取目錄或文件。 你是怎么上傳文件的? 如果您使用 multer 上傳 docx 文件,則需要在路徑或緩沖區選項中提供對上傳文件的引用:
mammoth.extractRawText({buffer: variable_that_holds_the_file.buffer})
.then(function(result){
var text = result.value; // The raw text
console.log(text);
})
.done();
否則,您需要修改路徑,因為它可能不正確。 要對此進行測試,請在__filename
內的 console.log 中使用__dirname
和或 __filename 來查看您的路徑在哪里。
使用下面的代碼它可能會幫助你。 它對我有用。
const mammoth = require("mammoth");
const path = require("path");
let filePath = path.join(__dirname,'./sampledocxfile.docx');
mammoth.extractRawText({path: filePath})
.then(function(result){
var html = result.value;
var messages = result.messages;
console.log(html);
})
.done();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.