0

I wrote a script that downloads file from web using file URL. I have an ActiveXObject of following type.

var objHTTP = new ActiveXObject("MSXML2.XMLHTTP");  
objHTTP.open("GET", strFileURL, false);

It works perfect for small size file says, file size less than 100MB. But when I try to download file with size greater than 100MB my script hanged. Then I tried,

objHTTP.open("GET", strFileURL, true);

but in this case we have to implement a callback function. I don't know how to implement callback and then use it. Can somebody help me. I am using TestComplete 7. Script that I wrote;

var objHTTP = new ActiveXObject("MSXML2.XMLHTTP"); 
  objHTTP.open("GET", strFileURL, true); 
  objHTTP.onreadystatechange = Callback; 
  objHTTP.send();  
  while((objHTTP.readyState != 4) && (objHTTP.readyState != 'complete'))  
  {   
    Delay(100); 
  }  
  if(200 != objHTTP.Status)  
  {  
    Log.Error("The " + strFileURL + " file was not found." + " The returned status is " + objHTTP.Status); 
    return;  
  }  

I don't know how to implement Callback function. Can somebody provide me implementation?

Thanks

Ali Ahmed
  • 1,749
  • 6
  • 20
  • 29

1 Answers1

0

Probably, the hanging is the result of the while loop waiting for a specific value of the readyState property. If the property never gets one of the expected values, the script will work forever.

I think the MSXML2.XMLHTTP object fails to load the large file, and never sets the readyState to one of the values your script expects. To understand what exactly is happening, I would check what value the property has after very long time, which is enough either for the file to load, or for the attempt to fail (say, 2 hours). If you know what value the readyState property has when the downloading fails, you can handle it in the script to avoid hanging.

That's it about the hanging itself. Now about the cause of the file downloading problem. I have found a page that tells about the problem and suggests setting higher timeouts - take a look: http://edgylogic.com/blog/downloading-large-files-vbscript/

The example is in VBScript, but it should be easy to implement the same approach with JScript. Please note that the example uses a different COM object - ServerXMLHTTP. You can read about it (including differences from XMLHTTP) here: http://msdn.microsoft.com/en-us/library/ms762278(v=VS.85).aspx

I hope this helps.

Alex
  • 587
  • 3
  • 9
  • Oh, sorry for not answering about the callback. Though I don't think using the callback function will solve the problem, using this approach may help you find out the value of the readyState property that is set when the downloading fails. So, it's simple: "objHTTP.onreadystatechange = OnReadyStateChange;". At that, you are expected to have the function declared in the same unit: function OnReadyStateChange() {/*objHTTP must be a global variable*/ Log.Message("readState changed to: " + objHTTP.readyState);} – Alex May 08 '11 at 15:46
  • Thanks for your reply. The problem is not in the while loop. The problem is with the send method, when I debugged the code I found that script hangs at the send method. It never reached even at the start of the while loop. – Ali Ahmed May 09 '11 at 05:56
  • Hmm... then it looks like a bug in the component. I would then try using the ServerXMLHTTP object - based on the description, it uses a different communication mechanism, so it may happen that the problem does not exist for it. Does this component work in your case? – Alex May 09 '11 at 07:47
  • I did not check ServerXMLHTTP. I will check and update. I am really thank for your valuable suggestion. – Ali Ahmed May 13 '11 at 07:11