Skip to main content

Content Starts Here

Download a file from the Internet

Article

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

VBScript

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)
  WEnd
  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

JScript

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);
  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;
  }
  var objADOStream = new ActiveXObject("ADODB.Stream");
  objADOStream.Open();
  objADOStream.Type = 1; //adTypeBinary

  objADOStream.Write(objHTTP.ResponseBody);
  objADOStream.Position = 0;    //Set the stream position to the start

  var objFSO = new ActiveXObject("Scripting.FileSystemObject");
  if (objFSO.FileExists(strHDLocation)) objFSO.DeleteFile(strHDLocation)
  objADOStream.SaveToFile(strHDLocation);
  objADOStream.Close();
}

Python



def test():
  #Specify the names of the source and destination files
  strFileURL = "http://www.automatedqa.com/file to get"
  strHDLocation = "c:\\temp\\filename"
  #Download the file
  objHTTP = Sys.OleObject["MSXML2.XMLHTTP"]
  objHTTP.open("GET", strFileURL, False)
  objHTTP.send()
  while((objHTTP.readyState != 4) and (objHTTP.readyState != 'complete')):
    Delay(100)
  if 200 != objHTTP.Status :
    Log.Error("The " + strFileURL + " file was not found." +" The returned status is " + objHTTP.Status)
    return
  objADOStream = Sys.OleObject["ADODB.Stream"]
  objADOStream.Open()
  objADOStream.Type = 1  #adTypeBinary
  
  objADOStream.Write(objHTTP.ResponseBody)
  objADOStream.Position = 0 #Set the stream position to the start
  objFSO = Sys.OleObject["Scripting.FileSystemObject"]
  if (objFSO.FileExists(strHDLocation)):
     objFSO.DeleteFile(strHDLocation)
  objADOStream.SaveToFile(strHDLocation)
  objADOStream.Close()
  
Previous MonthNext Month
SunMonTueWedThuFriSat