CODE A
I uploaded doc using multer and stored it in './data/documentUploads/' folder. then I used mammoth and cheerio library to read it in html form. here is code
xmlReader.getLibraryHTML = async docxFileName =>
new Promise((resolve, reject) => {
const filePath = './data/documentUploads/' + docxFileName;
mammoth.convertToHtml({path: filePath, ignoreEmptyParagraphs: true}, (err, result) => {
if (err) {
reject(err);
}
const html = result.value;
const $ = cheerio.load(html);
const tableRows = $('table tr');
const tableData = [];
tableRows.each((index, element) => {
const rowCells = $(element).find('td');
const rowData = [];
rowCells.each((index, element) => {
rowData.push(
$(element)
.toString()
.replace(/<\/p><\/td>|<\/?strong>|<p>|<td>/gi, '')
.replace(/<\/td>/gi, '')
.replace(/<\/p>|<br\s*\/?>/gi, '\n')
);
});
tableData.push(rowData);
});
resolve(tableData);
});
});
above function is used to read and convert word doc table in html form, i passed ms word file name to this function. and I called this function like this
const htmlString = await xmlReader.getLibraryHTML(fileName);
here is multer code
uploadDocxAccessor.uploadDocument = async req =>
new Promise((resolve, reject) => {
upload(req, {}, err => {
if (err instanceof multer.MulterError) {
const errorMsg = generateError(1002);
reject(errorMsg);
} else if (err) {
const errorMsg = generateError(1001);
reject(errorMsg);
} else {
resolve({
fileName: req.file.filename,
docxFileName: req.file.originalname
});
}
});
});
but it doesn't show any output or give any error. what is wrong in this
CODE B
But now I copied this code from somewhere , and that code is working fine here is that code: index.html file
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Reading Word Files</title>
</head>
<body style="padding: 50px;">
<form id="form" action="/fileupload" method="post" enctype="multipart/form-data">
<input type="file" id="docpicker" accept=".docx" required name="inputdoc" />
<input type="submit" />
</form>
</body>
</html>
Index.js file
let express = require("express");
let app = express();
const mammoth = require("mammoth");
const multer = require("multer");
const cheerio = require("cheerio");
const storage = multer.diskStorage({
destination: function (request, file, callback) {
callback(null, "./uploads/");
},
filename: function (request, file, callback) {
callback(null, "uploaded");
},
});
const upload = multer({ storage: storage });
app.use(express.json());
app.get("/", function (req, res) {
res.sendFile("/index.html", { root: "." });
});
app.post(
"/fileupload",
upload.single("inputdoc"),
async function (req, res, next) {
console.log("----", req.file.path);
mammoth
.convertToHtml({ path: req.file.path, ignoreEmptyParagraphs: true })
.then((result) => {
const html = result.value;
const $ = cheerio.load(html);
const tableRows = $("table tr");
const tableData = [];
tableRows.each((index, element) => {
const rowCells = $(element).find("td");
const rowData = [];
rowCells.each((index, element) => {
rowData.push(
$(element)
.toString()
.replace(/<\/p><\/td>|<\/?strong>|<p>|<td>/gi, "")
.replace(/<\/td>/gi, "")
.replace(/<\/p>|<br\s*\/?>/gi, "\n")
);
});
tableData.push(rowData);
});
res.send(tableData);
})
.catch((err) => {
console.error(err);
res.status(500).send("Error reading Word document");
});
}
);
const port = process.env.PORT || 8080;
app.listen(port, () => {
console.log("Server ran!");
});
In Code A there is no output or error In code A I get output as expected.
I want the code A working bcz i want the use promise.