0

I am using the below code to read a Word document file. This code worked fine when I used it to read a text file

 $('#xfilex').live('change', function() {
 var file = document.getElementById('xfilex').files[0];
 if(file) {
           var reader;
           reader = new FileReader();
           reader.readAsText(file, "UTF-8");
           reader.onload = loaded;
          }
 });
 function loaded(evt) {
                var fileString = evt.target.result;
                var str = fileString;
                alert(str);
 }   

But this code is unable to read .docx/.doc file. Please help me correct the code.

Exception
  • 8,111
  • 22
  • 85
  • 136
  • What happens when you run this on a .doc file, what happens when you run it on a docx file? What errors do you get? What exactly do you mean by "read", which part is performing the document conversion here? – Pekka Dec 28 '11 at 14:29
  • 2
    .doc is a proprietary file format, .docx is an compressed (IIRC) XML format. Both are not trivial to process using JavaScript alone. I would leave this up to a server to handle. – Pekka Dec 28 '11 at 15:16
  • @Pekka Thanks you so much for clarifying on this. Actually I don't have any access on server everything we do is like play in DOM( client side ) manner using jQuery.Thanks again. – Exception Dec 28 '11 at 15:55

1 Answers1

2

In order to read a DOCX file, you need to unzip its content (which is a mix of folders, xml files, and resources like images). Maybe you can have some clues in this post : Unzipping files

I doubt you can read a DOC file because it's a binary (and closed) format.

Community
  • 1
  • 1
CedX
  • 3,854
  • 2
  • 36
  • 45