4

Is there a native ActiveX Object or similar that I can use to download a source file straight to my HDD. Currently I'm using the following:

function downloadToFile(url, file) {
    var xhr = new ActiveXObject("msxml2.xmlhttp"), 
        ado = new ActiveXObject("ADODB.Stream");

    xhr.open("GET", url, false);
    xhr.send();
    if (xhr.status === 200) {
        ado.type = 1;
        ado.open();
        ado.write(xhr.responseBody);
        ado.saveToFile(file);
        ado.close();
    }
}

But this feels a bit inefficient for a few reasons:

I'm currently using two objects in place of what could possibly be a single object.

The entire response is stored in memory until its wrote to file. This isn't an issue for the most part until I use it to download fairly large files.

Notes/Edits:
I'm working from within microsoft's MSScriptControl.ScriptControl so many web-based libraries will not help.

I'm not necessarily looking for a single object if an answer is able to write the data to file as it is received.

SReject
  • 3,774
  • 1
  • 25
  • 41

1 Answers1

2

BITSAdmin

BITSAdmin is a Windows command-line tool for downloading and uploading files using the Background Intelligent Transfer Service (BITS).

Note: In Windows 7, BITSAdmin states it's deprecated in favor of PowerShell BITS cmdlets, and may not be included in future Windows versions.

Syntax:

bitsadmin /transfer job_name url local_name

JScript version:

var oShell = new ActiveXObject("WScript.Shell");
oShell.Run("bitsadmin /transfer myDownloadJob http://upload.wikimedia.org/wikipedia/en/b/bc/Wiki.png C:\\Work\\wikipedia-logo.png");

.NET System.Net.WebClient Class

If you have .NET Framework, you can register the System.Net.WebClient class for COM access:

C:\Windows\Microsoft.NET\Framework\v4.0.30319> regasm System.dll

and then use it like this:

var strURL = "http://upload.wikimedia.org/wikipedia/en/b/bc/Wiki.png";
var strFilePath = "C:\\Work\\wikipedia-logo.png";

var oWebClient = new ActiveXObject("System.Net.WebClient");
oWebClient.DownloadFile(strURL, strFilePath);

Chilkat HTTP Library

Chilkat HTTP ActiveX library (commercial) lets you download files directly:

var strURL = "http://upload.wikimedia.org/wikipedia/en/b/bc/Wiki.png";
var strFilePath = "C:\\Work\\wikipedia-logo.png";

var oHTTP = new ActiveXObject("Chilkat_9_5_0.Http");

//  Any string unlocks the component for the 1st 30-days.
var success = oHTTP.UnlockComponent("Anything for 30-day trial");
if (success != 1) {
  WScript.Echo(oHTTP.LastErrorText);
  WScript.Quit();
}

success = oHTTP.Download(strURL, strFilePath);
if (success != 1)
  WScript.Echo(oHTTP.LastErrorText);

cURL

Or how about shelling out to cURL (free, MIT/X derivate license)? Though I guess it counts as two objects because of WScript.Shell:

var oShell = new ActiveXObject("WScript.Shell");
oShell.Run("curl -o C:\\Work\\wikipedia-logo.png http://upload.wikimedia.org/wikipedia/en/b/bc/Wiki.png");
Helen
  • 87,344
  • 17
  • 243
  • 314
  • Im looking for a native windows object if possible – SReject Aug 29 '14 at 16:31
  • The update is a viable option, though I still feel there may be a better solution. If a more viable answer isn't presented you will get the bounty. – SReject Sep 02 '14 at 05:16