来自一个黑客脚本的分析:
Windows下如何下载一个文件?
很多人用WSscript调用"Microsoft.XMLHTTP"和ADO.Stream来下载文件。
strFileURL = "http://www.domain.com/file.zip"
strSavePath = "C:\somefolder\"
Set objXMLHTTP = CreateObject("MSXML2.XMLHTTP")
objXMLHTTP.open "GET", strFileURL, false
objXMLHTTP.send()
If objXMLHTTP.Status = 200 Then
Set objADOStream = CreateObject("ADODB.Stream")
objADOStream.Open
objADOStream.Type = 1
objADOStream.Write objXMLHTTP.ResponseBody
objADOStream.Position = 0
Set objFso = Createobject("Scripting.FileSystemObject")
If objFso.Fileexists(strSavePath) Then objFSO.DeleteFile strSavePath
Set objFso = Nothing
objADOStream.SaveToFile strSavePath
objADOStream.Close
Set objADOStream = Nothing
End if
Set objXMLHTTP = Nothing
JS代码:
function saveFile(sSourceUrl, sDestFile) {
var objXMLHTTP = new ActiveXObject("MSXML2.XMLHTTP");
objXMLHTTP.onreadystatechange=function() {
if (objXMLHTTP.readyState === 4) {
var objADOStream = new ActiveXObject("ADODB.Stream");
objADOStream.open();
objADOStream.type = 1;
objADOStream.write(objXMLHTTP.ResponseBody);
objADOStream.position = 0;
objADOStream.saveToFile(sDestFile, 2);
objADOStream.close();
}
};
objXMLHTTP.open("GET", sSourceUrl, false);
objXMLHTTP.send();
}
其实用windows更新自带的组件就可以下载文件了,简单的很:
bitsadmin /transfer 任务名 http://files.cnblogs.com/files/gayhub/bcn.js c:\bcn.bat
需要WinXP以上支持。