tags:

views:

25

answers:

1

I am creating file upload progress bar. I have a CGI script which copies the data, and here I increment the progress bar value by ONE after certain iterations. I am storing the incremented value in XML file (I also tried using plain text file). On the other side I have ajax reading incremented value from xml and depending on that it increments the DIV element.

However, what happens here is, it seems to me that although the ajax reads all the incremented values but it processes it after the CGI has finished execution. That is progress bar starts execution once the file copying and other stuff in CGI is completed. My code is:

AJAX::::

function polling_start() {   //GETS CALLED WHEN USER HITS FILE UPLOAD BUTTON
        intervalID = window.setInterval(send_request,100);
}
window.onload = function (){
        request  = initXMLHttpClient();
        request.overrideMimeType('text/xml');
        progress = document.getElementById('progress');
}

function initXMLHttpClient() {
        if (window.XMLHttpRequest){
        // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
        }
        else{
        // code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
return xmlhttp
}

function send_request()
{
request.open("GET","progress_bar.xml",true);
request.onreadystatechange = request_handler;
request.send();
}

function request_handler()
{
        if (request.readyState == 4 && request.status == 200)
        {
        var level=request.responseXML.getElementsByTagName('PROGRESS')[0].firstChild;
        progress.style.width = progress.innerHTML = level.nodeValue + '%';
        progress.style.backgroundColor = "green";
        }
}

/*************ON SERVER SIDE********************/

       char xmlDat1[] = "<DOCUMENT><PROGRESS>";
        char xmlDat2[] = "</PROGRESS></DOCUMENT>";

fptr = fopen("progress_bar.xml", "w");

.........OTHER STUFF..............................
.................................
                if(i == inc && j<=100)
                {
                fprintf(fptr, "%s\n", "<?xml version=\"1.0\"?>\n<!DOCTYPE DOCUMENT [\n<!ELEMENT DOCUMENT (PROGRESS)>\n<!ELEMENT PROGRESS (#PCDATA)>\n]>");
                //fprintf(fptr, "%s\n", "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"&gt;");
                fprintf(fptr, "%s", xmlDat1); //
                fprintf(fptr, "%d" ,j);
                fprintf(fptr, "%s" ,xmlDat2);
                fseek(fptr, 0, SEEK_SET);
                /*fprintf(fptr, "%d" ,j);
                fseek(fptr, 0, SEEK_SET);*/
                i = 0;
                //sleep(1);
                j++;
                }

(I also tried to write in .text, but same response)

Any quick response would be appreciable.

A: 

Just to add here: the data in the text/xml file is not available until file write completes(1 through 100 values are written for progress bar increment). My client side including Ajax is working fine only issue is that request.readyState == 4 happens only after all the data is written to the text file. Therefore I need a way where my updated data could be read by ajax simultaneously.

Thanks

Punit
Flush and close the file each time you write new status to it. You are leaving it open throughout the entire loop.
Remy Lebeau - TeamB
thanks for the response....however I tried that as well but same response......I tried using text file.....tried fork().....but no success till now :-(
Punit