0

I try to read file (id="file") after selected and return character count to paragraph (id="count"). I want it without submitting, without clicking any other button and without saving. I've tried jquery and "vanilla" javascript. Here is my code with "vanilla":

HTML:

<input type="file" id="file" name="file" onchange="fileCount(this)" />
<p id="count"></p>

Javascript:

function fileCount(file) {

    if (window.XMLHttpRequest) {
        // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    } else {
        // code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            document.getElementById("count").innerHTML = xmlhttp.responseText;

        }
    };
    xmlhttp.open('POST', 'fileCount.php', true);
    xmlhttp.setRequestHeader('X-FileName',file.name);
    xmlhttp.send(file);}

PHP:

if(!empty($_FILES['file'])) {

    if ($_FILES['file']['error'] === 0) {
        $temp = $_FILES['file']['tmp_name'];

        $ext = (explode(".",$temp));
        $ext = end($ext);
        $file = "";
        switch ($ext) {
            case "pdf":
                $text = pdf2text($temp);
                $file = strlen(utf8_decode($text));
                print_r( "<p id='count'>Počet: " . $file . "</p>");
                break;
            case "docx":
                $text = docx2text($temp);
                $file = strlen(utf8_decode($text));
                print_r( "<p id='count'>Počet: " . $file . "</p>");
                break;
            case "odt":
                $text = odt2text($temp);
                $file = strlen(utf8_decode($text));
                print_r( "<p id='count'>Počet: " . $file . "</p>");
                break;
            case "rtf":
                $text = rtf2text($temp);
                $file = strlen(utf8_decode($text));
                print_r( "<p id='count'>Počet: " . $file . "</p>");
                break;
            //case "txt":
                $text = file_get_contents($temp);
                $file = strlen(utf8_decode($text));
                print_r( "<p id='count'>Počet: " . $file . "</p>");
                break;
            default:
                print_r( "Wrong file format" );
        }
    }

}

and nothing is returning. Please help, what is wrong. Thank you

Nini Vanini
  • 25
  • 1
  • 7
  • You can't just post a file like that. You need to look in to [FormData](http://stackoverflow.com/questions/21044798/how-to-use-formdata-for-ajax-file-upload). And if you already have the `

    ` tag on you page, you don't need to return the tag in your response.
    – Magnus Eriksson Jun 22 '16 at 13:41
  • @MagnusEriksson The OP is writing code targeting a browser from (literally) the last century, i doubt FormData is available! – Steve Jun 22 '16 at 13:46
  • @Steve - I can't see that he mentions that anywhere? Looking at the code, I'm assuming that the script is a copied/pasted from somewhere else. But I can be wrong and you make a good point. (I know that I'm probably making an ass out of u and me by assuming) – Magnus Eriksson Jun 22 '16 at 13:49
  • 1
    @MagnusEriksson no, im being the ass, i am just amazed how many people copy that damned snippet without even reading it.. OP if your are targeting modern browsers, then filereader could be an option, no need for the server post http://stackoverflow.com/questions/27522979/read-a-local-textfile-using-javascript – Steve Jun 22 '16 at 13:55

0 Answers0