[Collapse]TestComplete How To
 [Expand]Microsoft Office
 [Expand]TestComplete Utility Scripts
 [Expand]Third-party Controls
 [Collapse]Web Utility Scripts
   Click a cell located in a specific HTML table row/viewarticle/24768/
   Convert a file to Base64/viewarticle/19898/
   Convert Base64-encoded string to a file/viewarticle/42195/
   Download a file from the Internet/viewarticle/8999/
   Get the page loading time/viewarticle/9043/
   Send an e-mail message/viewarticle/9020/
   Stripping HTML Tags using Regular Expressions/viewarticle/54652/
   Verify whether a web link is valid/viewarticle/9000/
   Wait for download completion in Chrome/viewarticle/51660/
 [Expand]Windows Utility Scripts
Updated: 4/24/2012 Applies To: TestComplete Rating: Not badNot badNot badNot badNot bad 23 vote(s) Click to rate: PoorNot badAverageGoodExcellent
Download a file from the Internet

The script below downloads a file from the Internet and saves it to the specified directory on your computer.

VBScriptCopy Code

Sub Test
  Dim strFileURL, strHDLocation, objHTTP, objADOStream, objFSO 
  ' Specify the names of the source and destination files
  strFileURL = "http://www.automatedqa.com/file to get"
  strHDLocation = "c:\temp\filename"
  ' Download the file 
  Set objHTTP = Sys.OleObject("MSXML2.XMLHTTP") 
  Call objHTTP.open("GET", strFileURL, False)
  Call objHTTP.send
  While ( objHTTP.readyState <> 4 )  And ( objHTTP.readyState <> "complete" ) 
    Call Delay(100)
  If (200 <> objHTTP.Status) Then
    Call Log.Error("The " & strFileURL & " file was not found." &_ 
                   " The returned status is " & objHTTP.Status)
    Exit Sub 
  End If
  Set objADOStream = Sys.OleObject("ADODB.Stream") 
  Call objADOStream.Open
  objADOStream.Type = 1 ' adTypeBinary
  Call objADOStream.Write(objHTTP.ResponseBody)
  objADOStream.Position = 0 ' Set the stream position to the start
  Set objFSO = Sys.OleObject("Scripting.FileSystemObject") 
  If (objFSO.FileExists(strHDLocation)) Then
    Call objFSO.DeleteFile(strHDLocation)
  End If
  Call objADOStream.SaveToFile(strHDLocation)
  Call objADOStream.Close
End Sub

JScriptCopy Code

function Test()
  // Specify the names of the source and destination files
  var strFileURL = "http://www.automatedqa.com/file to get";
  var strHDLocation = "c:\\temp\\filename";
  // Download the file
  var objHTTP = new ActiveXObject("MSXML2.XMLHTTP");
  objHTTP.open("GET", strFileURL, false);
  while((objHTTP.readyState != 4) && (objHTTP.readyState != 'complete')) { 
  if (200 != objHTTP.Status) {
    Log.Error("The " + strFileURL + " file was not found." +
              " The returned status is " + objHTTP.Status);
  var objADOStream = new ActiveXObject("ADODB.Stream");
  objADOStream.Type = 1; //adTypeBinary
  objADOStream.Position = 0;    //Set the stream position to the start
  var objFSO = new ActiveXObject("Scripting.FileSystemObject");
  if (objFSO.FileExists(strHDLocation)) objFSO.DeleteFile(strHDLocation)

© 2020 SmartBear Software. All rights reserved.
Email Send feedback on this document