1

I want to count words from below file types.. ['pdf','xls','xlsx','odt','ppt','pptx','txt','doc','docx','rtf']/ currently my code reads text files only.. please help me for other file types..

Below is my code..

<script>
 $('#file').change( function(event) {
    var imgpath=document.getElementById('file');
    if (!imgpath.value==""){
    var ext = imgpath.value.split('.').pop().toLowerCase(); 
        if($.inArray(ext, ['pdf','xls','xlsx','odt','ppt','pptx','txt','doc','docx','rtf']) != -1) {
        var f = event.target.files[0];
        if (f) {
            var r = new FileReader();
            r.onload = function(e) { 
                var strings = "";
                var contents = e.target.result; alert(contents);
                var words = contents.match(/\S+/g).length; 
                $('#display_file_count').text(words);
            }
            r.readAsText(f);
        }
        }else{
            alert('file type not supported.');
            $('#file').val('');
        }
    }
});
 </script>
  • You are trying to could the file types that are infact elements of array, you can simply use .length – Adil Feb 26 '16 at 09:49
  • 1
    Some of those files cannot be accessed in the same manner as a text file as they are not plain text (pdf, xls, xlsx, odt, ppt, pptx, doc and docx for example). What you are attempting to do is not a trivial task, and not one suited to JS, assuming its even possible at all. – Rory McCrossan Feb 26 '16 at 09:52
  • You need a reader for each type, to parse the file and outputs the words! – Oden Feb 26 '16 at 09:53
  • the "x" file formats you list are XML, which JS can easily search: `.documentElement.textContent.match(/\b\w+\b/g)`. – dandavis Feb 26 '16 at 10:13

0 Answers0