Search code examples
jqueryhtmlfileapi

How to modify the current code to read .docx file using HTML5 file api


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.


Solution

  • 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.