论坛交流
首页办公自动化| 网页制作| 平面设计| 动画制作| 数据库开发| 程序设计| 全部视频教程
应用视频: Windows | Word2007 | Excel2007 | PowerPoint2007 | Dreamweaver 8 | Fireworks 8 | Flash 8 | Photoshop cs | CorelDraw 12
编程视频: C语言视频教程 | HTML | Div+Css布局 | Javascript | Access数据库 | Asp | Sql Server数据库Asp.net  | Flash AS
当前位置 > 文字教程 > Asp教程
Tag:入门,文摘,实例,技巧,iis,表单,对象,上传,数据库,记录集,session,cookies,存储过程,注入,分页,安全,优化,xmlhttp,fso,jmail,application,防盗链,stream,组件,md5,乱码,缓存,加密,验证码,算法,ubb,正则表达式,水印,,日志,压缩,url重写,控件,函数,破解,触发器,socket,ADO,初学,聊天室,留言本,视频教程

实现ASP网站程序自动升级的方法

文章类别:Asp | 发表日期:2009-7-16 11:29:45

实现ASP网站程序自动升级的方法

现在流行虚拟主机建站,但是网站程序每次升级的时候颇为麻烦:先去官方看公告,然后下载升级包到本地,解压,FTP上传到虚拟主机。但是这样太麻烦了,所以研究了一套程序能够自动升级的办法,希望对ASP程序开发者有帮助.....

先看看传统的win32程序的升级过程(比如杀毒软件),它是依靠软件的升级程序通过网络连接到服务器分析并下载升级文件到本地。

WEB程序有点不一样,因为它是运行于WEB服务器。它最终是要把升级服务器上的文件覆盖到WEB服务器,站长的电脑只是中转。如果直接把升级服务器上的文件Copy到WEB服务器(而不通过站长中转)那就实现了自动升级。

好在系统自带了一个 Microsoft.XMLHTTP 组件用于访问WEB,在ASP中可以调用它来实现连接升级服务器下载升级文件。

以下代码是利用 Microsoft.XMLHTTP下载文件的例子:

<%
Set xPost = CreateObject("Microsoft.XMLHTTP")
         xPost.Open "GET","http://www.xxx.com/test.exe",False
         xPost.Send()
         Set sGet = CreateObject("ADODB.Stream")
         sGet.Mode = 3
         sGet.Type = 1
         sGet.Open()
         sGet.Write(xPost.responseBody)
         sGet.SaveToFile Server.MapPath("update.exe"),2
         set sGet = nothing
         set sPOST = nothing
         response.Write("下载文件成功!<br>")
%>

上面代码就是把 http://www.xxx.org/test.exe保存到WEB服务器当前目录,至于Microsoft.XMLHTTP 的更多用法还是看看MSDN吧。

如果文件比较多,就会多次调用Microsoft.XMLHTTP连接网络,就可能出现某次连接失败部分文件未能更新的情况,为了避免这种情况,最好是把所有文件打包为一个文件一次下载到WEB后再解包。这里说的打包可不是RAR或者ZIP包,而是我们自己定义。比如把所有文件拼接为一个,然后再根据特殊的记号分开。现在没这么麻烦咯,因为有个现成的办法,我们使用拿来主义就是:把所有文件(二进制形式)及其路径信息放入Access数据库。

下面这个vbs文件(来自海洋顶端2006Plus)就是打包当前目录的所有文件的:

   Dim n, ws, fsoX, thePath
         Set ws = CreateObject("WScript.Shell")
         Set fsoX = CreateObject("Scripting.FileSystemObject")
         thePath = ws.Exec("cmd /c cd").StdOut.ReadAll() & "\"
         i = InStr(thePath, Chr(13))
         thePath = Left(thePath, i - 1)
         n = len(thePath)
On Error Resume Next
         addToMdb(thePath)
         Wscript.Echo "当前目录已经打包完毕,根目录为当前目录"
         Sub addToMdb(thePath)
                   Dim rs, conn, stream, connStr
                   Set rs = CreateObject("ADODB.RecordSet")
                   Set stream = CreateObject("ADODB.Stream")
                   Set conn = CreateObject("ADODB.Connection")
                   Set adoCatalog = CreateObject("ADOX.Catalog")
                   connStr = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=Packet.mdb"
                   adoCatalog.Create connStr
                   conn.Open connStr
                   conn.Execute("Create Table FileData(Id int IDENTITY(0,1) PRIMARY KEY CLUSTERED, P Text, fileContent Image)")
                   stream.Open
                   stream.Type = 1
                  rs.Open "FileData", conn, 3, 3
                   fsoTreeForMdb thePath, rs, stream
                   rs.Close
                   Conn.Close
                   stream.Close
                   Set rs = Nothing
                   Set conn = Nothing
                   Set stream = Nothing
                   Set adoCatalog = Nothing
         End Sub
         Function fsoTreeForMdb(thePath, rs, stream)
                   Dim i, item, theFolder, folders, files
                   sysFileList = "$" & WScript.ScriptName & "$Packet.mdb$Packet.ldb$"
                   Set theFolder = fsoX.GetFolder(thePath)
                   Set files = theFolder.Files
                   Set folders = theFolder.SubFolders
                   For Each item In folders
                            fsoTreeForMdb item.Path, rs, stream
                   Next
                   For Each item In files
                            If InStr(LCase(sysFileList), "$" & LCase(item.Name) & "$") <= 0 Then
                                     rs.AddNew
                                     rs("P") = Mid(item.Path, n + 2)
                                     stream.LoadFromFile(item.Path)
                                     rs("fileContent") = stream.Read()
                                     rs.Update
                            End If
                   Next
                   Set files = Nothing
                   Set folders = Nothing
                   Set theFolder = Nothing
         End Function
以下是解包的ASP文件:
<%
         Sub UnPack()
                   str = Server.MapPath(".") & "\"
                   Set rs = CreateObject("ADODB.RecordSet")
                   Set stream = CreateObject("ADODB.Stream")
                   Set conn = CreateObject("ADODB.Connection")
                   Set oFso = CreateObject("Scripting.FileSystemObject")
                   connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("update.mdb")
                   conn.Open connStr
                   rs.Open "FileData", conn, 1, 1
                   stream.Open
                   stream.Type = 1
                   Do Until rs.Eof
                            theFolder = Left(rs("P"), InStrRev(rs("P"), "\"))
                            If oFso.FolderExists(str & theFolder) = False Then
                                     oFso.CreateFolder(str & theFolder)
                            End If
                            stream.SetEOS()
                            If IsNull(rs("fileContent")) = False Then stream.Write rs("fileContent")
                            stream.SaveToFile str & rs("P"), 2
                            rs.MoveNext
                   Loop
                   rs.Close
                   conn.Close
                   stream.Close
                   Set ws = Nothing
                   Set rs = Nothing
                   Set stream = Nothing
                   Set conn = Nothing
                   Set oFso = Nothing
         End Sub
%>

嗯,有了以上代码就不难开发出自己的ASP升级程序了,流程无外乎这样:判断是否需要升级(Y) -> 下载升级包 -> 解开升级包覆盖旧文件 -> 删除升级包 -> 更新版本信息 ->

 

视频教程列表
文章教程搜索
 
Asp推荐教程
Asp热门教程
看全部视频教程
购买方式/价格
购买视频教程: 咨询客服
tel:15972130058