我正在尝试使用
Windows脚本宿主(JScript)自动下载文件.我看到ADODB.Stream有一个Open方法,其文档似乎应该可以打开HTTP URL并流式传输响应体:
var url = 'http://example.com/example.tar.gz'; var path = 'example.tar.gz'; var input = WScript.CreateObject('ADODB.Stream'); input.Open(url); input.SaveToFile(path); input.Close();
但它在公开电话上遭到轰炸
(null):在此操作的范围内未找到与名称,范围或选择条件匹配的对象或数据.
这是JScript中的下载代码.还添加了一些API信息的引用.
var Source = WScript.Arguments.Item(0); var Target = WScript.Arguments.Item(1); var Object = WScript.CreateObject('MSXML2.XMLHTTP'); Object.Open('GET',Source,false); Object.Send(); if (Object.Status == 200) { // Create the Data Stream var Stream = WScript.CreateObject('ADODB.Stream'); // Establish the Stream Stream.Open(); Stream.Type = 1; // adTypeBinary Stream.Write(Object.ResponseBody); Stream.Position = 0; // Create an Empty Target File var File = WScript.CreateObject('Scripting.FileSystemObject'); if (File.FileExists(Target)) { File.DeleteFile(Target); } // Write the Data Stream to the File Stream.SaveToFile(Target,2); // adSaveCreateOverWrite Stream.Close(); }
ADODB流:
> http://www.w3schools.com/ado/ado_ref_stream.asp
> http://msdn.microsoft.com/en-us/library/windows/desktop/ms675032.aspx
> http://msdn.microsoft.com/en-us/library/windows/desktop/ms680846.aspx
Scripting.FileSystemObject的:
> http://www.w3schools.com/asp/asp_ref_filesystem.asp