ActiveXObject to download directly to hard drive
Is there a native ActiveX object or similar that I can use to download the source file directly to my hard drive. I am currently 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 is a bit inefficient for several reasons:
I am currently using two objects instead of what might be a single object.
The entire response is kept in memory until it is written to a file. This is not a problem for the most part, until I use it to upload fairly large files.
Notes / edits :
I am working from within microsoft MSScriptControl.ScriptControl so many web libraries won't help.
I'm not necessarily looking for a single object if the answer is capable of writing data to a file as it is received.
source to share
BITSAdmin
BITSAdmin
is a Windows command line tool for downloading and uploading files using the Background Intelligent Transfer Service (BITS).
Note. Windows 7 BITSAdmin
states that it is deprecated in favor of BITS PowerShell cmdlets and may not be included in future versions of Windows.
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 the .NET Framework, you can register the class System.Net.WebClient
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) allows you to download 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 cURL wrapping (free, MIT / X license)? Though I'm guessing it counts as two entities due to 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");
source to share