0

Searching for the solution on situation where I upload a word document and try to read the content of the word document. Perfect example is when you go to apply for a job and you have to upload your resume and behind the scene it uploads the word document and parse your name, address, city, state, country, college, company name etc and set it exactly in the appropriate fields. I am trying to accomplish the exact same thing.

Basically stuck where I upload either word or excel document and it is showing me garbage characters. However when I upload text file, it works fine.

Here is Javascript code:

const File = 
document.getElementById("file");
const Btn = 
document.getElementById("button");
const Txt = 
document.getElementById("text");

Btn.addEventListener("click", function() {
File.click();
});

File.addEventListener("change", function() 
{

if (File.value) {
Txt.innerHTML = File.value.match(
  /[\/\\]([\w\d\s\.\-\(\)]+)$/
)[1];
} else {
Txt.innerHTML = "No file chosen, yet.";
}

const file = 
document.getElementById("file").files[0];
if (file) {
  const reader = new FileReader();
  reader.readAsText(file);
  reader.onload = function (evt) {

document.getElementById("data").innerHTML = 
evt.target.result;
  }
  reader.onerror = function (evt) {

document.getElementById("data").innerHTML = 
"error reading file";
  }
}
});

Here is the HTML code:

<!DOCTYPE html>
<html lang="en">
<head>

<title>Document</title>
</head>
<body>
<input type="file" id="file" 
hidden="hidden" />
<button type="button" 
id="button">Upload</button>
<span id="text">Upload a Resume</span>
<p id='data'></p>
<script src="openfile.js"></script>
</body>
</html>

Here is how the garbage character are showing when I upload word or excel document:

word document:

Garbage characters showing when upload word document

Excel Document:

Garbage characters showing when upload excel document

Text file:

Text file is fine

I ransack every option where I can find an example of this sort of problem. I tried to find the source code on random companies website where they have this functionality build but it was also a complete dud. Any help. Thanks in advance.

FileReader() function is reading garbage characters. It can only read text file but cant able to read doc/excel content?

Sufian
  • 1
  • 4
  • Possible duplicate of [How do I render a Word document (.doc, .docx) in the browser using JavaScript?](https://stackoverflow.com/questions/27957766/how-do-i-render-a-word-document-doc-docx-in-the-browser-using-javascript) – Gerard Oct 31 '18 at 08:36
  • the solution is not at all applicable to the above problem. It is completely different problem. – Sufian Oct 31 '18 at 08:58

0 Answers0