簡體   English   中英

為什么在使用“mammoth”轉換簡單的 .docx 文件時沒有出現此類文件錯誤?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM