Getting XMLHttpRequest Progress from a PHP Script

I am using javascript to run the XMLHttpRequest for a PHP script that returns data. Basically, I want the user to have a progress indicator (instead of a circular circle or something else) that shows the progress of getting and receiving data. I know if I get the file, I can just check the header for the length of the content and use it, but in the case of a script you don't know how much data it retrieves.

The answer to this may be as simple as β€œthis is impossible,” because now it seems that it is. But in conclusion: how do you track the progress of the script (php) over and XMLHttpRequest?

+5
source share
4 answers

If you use FireFox (and I'm sure most browsers other than IE), then there really is a way to tell how much data was transferred during the XHR operation. If the operation in question sends the correct header, it is quite simple to use this information to calculate the percentage of downloaded data.

I wrote this code to determine the percentage of data transferred to an XHR operation several years ago, so I apologize for not reflecting the years of coding experience I have received since then. I almost certainly will not write it like this now! Nevertheless, I managed to catch him and hope that he will be useful to you.

, , IE7 Explorer, , , , IE. 8 9, , . IE, , !

, beforeSend ( jQuery , ajax-), Javascript ( , 50 , , , 200 , ). , , , responseText XHR. responseText , . , length() string, , .

, , , . , . , . , , .

<script type="text/javascript">
$.ajax ({
    beforeSend  : function (thisXHR)
    {
        // IE doesn't support responseText access in interactive mode
        if (!$.browser.msie)
        {
            myTrigger = setInterval (function ()
            {
                if (thisXHR.readyState > 2)
                // When there is partial data available use it to determine how much of the document is downloaded
                {   
                    var dlBytes = thisXHR.responseText.length;
                    if (totalBytes == -1)
                        totalBytes  = thisXHR.getResponseHeader ('Content-length');
                    (totalBytes > 0)?
                        $('#progress').html (Math.round ((dlBytes / totalBytes) * 100) + "%"):
                        $('#progress').html (Math.round (dlBytes / 1024) + "K");
                }
            }, 50); // Check the status every 50 miliseconds
        }
    },
    complete    : function ()
    {
        // Kill the download progress polling timer
        if (myTrigger)
            clearInterval (myTrigger);
    }
});
</script>
+2

2 ajax. , , - . , 2 ajax .

PHP ( job.php), $_SESSION ['job_progress'] .

PHP script ( progress.php), , , ..

<?php echo $_SESSION['job_progress'];

ajax job.php. ajax progress.php, 3 . .

ajax, job.php . ajax ping progress.php script.

+1

(firefox ) onreadystatechange readyState 3 (.. ) , .

, responseText , , , .

Internet Explorer ( , IE7, ) , responseText responseBody readyState 3. , IE onreadystatechange readyState 3, , .

0

. (, ). UID , .

- ( , ..) SID + UID, .

ajax SID + UID.

* Perhaps you can only get the UID, but I found that it is more manageable when you can also control tasks by the user / session.

0
source

All Articles