Forum Discussion

pantoniotti's avatar
pantoniotti
New Contributor
15 years ago

Script to download a file from the web

Hi there,
 



I am new to TestComplete and can't find any info on how to write a script to
download a file from the internet.

I tried the following :

 


 

but that does not do anything.

Anyone knows how to do this ?




Thanks

Philippe


1 Reply

  • Hi Philippe,




    This method can be used with files which reside on a local or a network drive. It cannot download files from the Internet.




    You can download a file by using the script below. It sends an HTTP request by using the XMLHTTP ActiveX object (it's also possible to use the WinHttpRequest ActiveX object instead).




    [JScript, C#Script, C++Script]




        // Specify the source and destination file names


        var strHDLocation = "c:\\temp\\filename";




        // Download the file

        var objHTTP = new ActiveXObject("MSXML2.XMLHTTP");

        //or

        //var objHTTP = new ActiveXObject("WinHttp.WinHttpRequest.5.1");

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

        objHTTP.send();




        if (objHTTP.Status == 200)

        {

          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();

        }