title: JScript/VBscript: Tracking file download progress via XMLHttpRequest
body: <p>I work on a JScript/VBScript HTML application (so we can execute at higher privileges than a normal web page) and we're providing the capability to download a file and need to provide feedback on the download progress to our users.  Naturally, I figured a progress bar or progress text based on the total file size and the amount of the file already downloaded.  I'm trying to get the size of the already downloaded portion in Internet Explorer. Thus far, I've tried everything I can think of to access how much of the file is already downloaded.  When trying to access the responseText or responseBody during readyState 3 (interactive), I simply get the exception: </p>
  
  <blockquote>
    <p>The data necessary to complete this operation is not yet available.</p>
  </blockquote>
  
  <p>So it appears that Microsoft was not lying when they documented that responseText or responseBody will not be available until the XMLHttpRequest has completed (gone to readyState 4)</p>
  
  <p>I can get the total file size and save the file to disk, so I've omitted those parts of the code I already have.  Here's what I've been trying.  I'm open to any other suggestions -- even changing implementations away from XMLHttpRequest.  Note that I do not have control over the server side of things... only my client app, so php/ASP solutions won't work.
  Thanks in advance...</p>
  
  <pre><code>function getRemoteFile(urlToFile){
    if (urlToFile)    {
      try         {
        var xmlReq = new XMLHttpRequest();
        //Tried all of the following with no luck
      //var xmlReq = new ActiveXObject('Microsoft.XMLHTTP');
      //var xmlReq = new ActiveXObject('Msxml2.DOMDocument.3.0');
      //var xmlReq = new ActiveXObject("Msxml2.XMLHTTP.3.0");
      //var xmlReq = new ActiveXObject("Msxml2.XMLHTTP.6.0");
      //var xmlReq =new ActiveXObject("Msxml2.DOMDocument.6.0");
  
        function updateDownloadProgress() {                     
          if (xmlReq.readyState == 3) {
              try {
                alert(xmlReq.responseText.length);
  
              }
              catch (e) {
              alert(e.message);
              }          
            window.setTimeout(updateDownloadProgress, 200);
          }
        }                    
  
        xmlReq.open("GET", urlToFile, true);
        xmlReq.onreadystatechange = function(){
          if (xmlReq.readyState == 3)      {
            updateDownloadProgress();
          }
          if (xmlReq.readyState == 4)      {
            alert("done");
          }
        }
        xmlReq.send(null);
      } catch(e) {
        alert(e.message);
        return null;
      }
    }
  }
  </code></pre>