Skip to content

Instantly share code, notes, and snippets.

@katahiromz
Created February 20, 2019 03:45
Show Gist options
  • Save katahiromz/5a97709580bf64ccee7628e7a3e5afee to your computer and use it in GitHub Desktop.
Save katahiromz/5a97709580bf64ccee7628e7a3e5afee to your computer and use it in GitHub Desktop.
download.js
// download.js --- download a file from internet
//===========================================
// TODO: Please specify the URL and filename:
var url = "https://www.google.com/robots.txt"
var filename = "robots.txt"
// TODO: OK, save and open this file. Done.
//===========================================
function CreateHttpRequest() {
var ids = new Array(
"WinHttp.WinHttpRequest.5.1",
"WinHttp.WinHttpRequest.5",
"WinHttp.WinHttpRequest",
"Msxml2.ServerXMLHTTP.6.0",
"Msxml2.ServerXMLHTTP.5.0",
"Msxml2.ServerXMLHTTP.4.0",
"Msxml2.ServerXMLHTTP.3.0",
"Msxml2.ServerXMLHTTP",
"Microsoft.ServerXMLHTTP",
"Msxml2.XMLHTTP.6.0",
"Msxml2.XMLHTTP.5.0",
"Msxml2.XMLHTTP.4.0",
"Msxml2.XMLHTTP.3.0",
"Msxml2.XMLHTTP",
"Microsoft.XMLHTTP");
for (var i = 0; i < ids.length; ++i) {
var obj = new ActiveXObject(ids[i]);
if (obj) return obj;
}
return null;
}
function DoDownload(Url, Path) {
var req = CreateHttpRequest();
if (!req) {
WScript.Echo("ERROR: Can't create an HTTP object!");
return;
}
req.Open("GET", Url, false);
req.setRequestHeader("Pragma", "no-cache");
req.setRequestHeader("Cache-Control", "no-cache");
req.setRequestHeader("If-Modified-Since", "Thu, 01 Jun 1970 00:00:00 GMT");
req.Send();
switch (req.Status) {
case 200:
var stream = new ActiveXObject("ADODB.Stream");
stream.Type = 1;
stream.Open();
stream.Write(req.responseBody);
stream.SaveToFile(Path, 2);
stream.Close();
WScript.Echo("OK, downloaded!");
break;
default:
WScript.Echo("ERROR: Code " + req.Status);
}
}
DoDownload(url, filename);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment