●Description: ASP online upgrade class ●Version: 1.0.0
●Author: Xiao Yuehen (xiaoyuehen)
●MSN: xiaoyuehen(at)msn.com
●Please replace (at) with @●Copyright: Since it is shared, copyright does not matter. But it must be limited to online dissemination and cannot be used in traditional media!
●I would be even more grateful if you could keep these instructions!
●If you have better code optimization and related improvements, please remember to tell me, thank you very much!
●Ideas:
1. Query the version list => 2. Compare version differences => 3. Get the update list of the next higher version, if there is no higher version, jump to step 5 => 4. Update => return to step 3
5. Exit update ●Other instructions: Incremental upgrade.
●Off-topic: It took about 7 hours in total. It was a bit rushed and the code was not refined enough. During local testing, it took nearly 1 second to update two versions and a total of 4 files.
I have never done anything similar before, so I can’t talk about any algorithm. Friends who have done it, please give me your opinions. Thank you!
●This code is intended to communicate with each other●
●Before you begin, please read the following instructions carefully.
●Server side requirements:
1. Site Manager, you can access the version and related upgrade information through the URL address.
2. Version information file, such as Version.asp
3. Each version directory must be under the directory specified by UrlUpdate (see description below). For example: UrlUpdate is http://Localhost/__Jxc/Update/ ,
If Version is 1.0.8, the upgrade file of this version must be located at http://Localhost/__Jxc/Update/108/ .
4. The information returned by the version information is a list, each line represents a version information (no blank lines are allowed), and the higher version is at the top. The format is as follows:
1.1.0
1.0.8
1.0.0
5. The file update information format of a certain version is to remove the number after the number + FileType (see description below), and place it under UrlUpdate, such as: http://Localhost/__Jxc/Update/110.asp , and its content format is as follows:
3.htm|Test/Test/3.asp
4.htm|Test/Test/4.asp
5.htm|Test/5.asp
6.htm|Test/6.asp
Separate the source file and destination file with |. The source file will be read from the corresponding version directory, as shown above, the corresponding address of 3.htm should be
http://Localhost/__Jxc/Update/110/3.htm
If UpdateLocalPath = "/", the corresponding update destination of Test/Test/3.asp is /Test/Test/3.asp. During the update process, the program will automatically create a non-existent directory.
And overwrite the target file
●Client requirements:
IIS 5.0 or above
FSO support (for generating files)
Adodb.Stream support (for encoding conversion)
Microsoft.XMLHTTP support (for remote retrieval of information)
●Attributes:
Info Get the last information during the upgrade process ●Parameter description:
UrlVersion ●Required● Complete URL of version information, starting with http://
UrlUpdate ●Required● Upgrade URL, starting with http:// and ending with /
UpdateLocalPath ●Required● Local update directory, starting with / and ending with /. Starting with / is for updating the current site. Prevent writing to other directories. ●Default value● /
UrlHistory ●Required● File name of the generated software history file
LocalVersion ●Required● Current version information ●Default value● 1.0.0
FileType ●Required● Version information suffix ●Default value● .asp
●Method description:
doUpdate upgrade
After the relevant parameters are set, you can start the long level in this way. ●Other instructions: The version number must be composed of numbers 0-9 and ., and the first digit cannot be less than 1. The length of each version number must be the same. For example, 1.0 .0 and 1.2.2 or 1.2.04 and 1.2.78
●Example:
Program code
<!--#include file="../__Inc/Cls_OnlineUpdate.asp"-->
<%
Dim objUpdate
Set objUpdate = New Cls_oUpdate
With objUpdate
.UrlVersion = " http://Localhost/__Jxc/Update/Version.asp "
.UrlUpdate = " http://Localhost/__Jxc/Update/ "
.UpdateLocalPath = "/"
.LocalVersion = "1.0.0"
.doUpdate
response.Write(.Info)
End With
Set objUpdate = Nothing
%>
Class files:
program code
<%
Rem ############################################### ####################################
Rem ## Online upgrade class statement
Class Cls_oUpdate
Rem ############################################### ################
Rem ## Description: ASP online upgrade class
Rem ## Version: 1.0.0
Rem ## Author: Xiao Yuehen
Rem ## MSN: xiaoyuehen(at)msn.com
Rem ## Please replace (at) with @
Rem ## Copyright: Since it is shared, there is no copyright. But it must be limited to online dissemination and cannot be used in traditional media!
Rem ## If you can keep these instructions, I would be even more grateful!
Rem ## If you have better code optimization and related improvements, please remember to tell me, thank you very much!
Rem ############################################### ################
Public LocalVersion, LastVersion, FileType
Public UrlVersion, UrlUpdate, UpdateLocalPath, Info
Public UrlHistory
Private sstrVersionList, sarrVersionList, sintLocalVersion, sstrLocalVersion
Private sstrLogContent, sstrHistoryContent, sstrUrlUpdate, sstrUrlLocal
Rem ############################################### ################
Private Sub Class_Initialize()
Rem ## Complete URL of version information, starting with http://
Rem ## Example: http://localhost/software/Version.htm
UrlVersion = ""
Rem ## Upgrade URL, starting with http:// and ending with /
Rem ## Example: http://localhost/software/
UrlUpdate = ""
Rem ## Local update directory, starting with / and ending with /. Starting with / is for updating the current site. Prevent writing to other directories.
Rem ## The program will check whether the directory exists. If it does not exist, it will be created automatically.
UpdateLocalPath = "/"
Rem ## Generated software history file
UrlHistory = "history.htm"
Rem ## Last prompt message
Info = ""
Rem ## Current version
LocalVersion = "1.0.0"
Rem ## latest version
LastVersion = "1.0.0"
Rem ## The suffix name of each version information file
FileType = ".asp"
End Sub
Rem ############################################### ################
Rem ############################################### ################
Private Sub Class_Terminate()
End Sub
Rem ############################################### ################
Rem ## Perform upgrade action
Rem ############################################### ################
Public function doUpdate()
doUpdate = False
UrlVersion = Trim(UrlVersion)
UrlUpdate = Trim(UrlUpdate)
Rem ## Upgrade URL detection
If (Left(UrlVersion, 7) <> "http://") or (Left(UrlUpdate, 7) <> "http://") Then
Info = "The version detection URL is empty, the upgrade URL is empty or has the wrong format (#1)"
Exit function
End If
If Right(UrlUpdate, 1) <> "/" Then
sstrUrlUpdate = UrlUpdate & "/"
Else
sstrUrlUpdate = UrlUpdate
End If
If Right(UpdateLocalPath, 1) <> "/" Then
sstrUrlLocal = UpdateLocalPath & "/"
Else
sstrUrlLocal = UpdateLocalPath
End If
Rem ## Current version information (number)
sstrLocalVersion = LocalVersion
sintLocalVersion = Replace(sstrLocalVersion, ".", "")
sintLocalVersion = toNum(sintLocalVersion, 0)
Rem ## Version detection (initialize version information and compare)
If IsLastVersion Then Exit function
Rem ## Start upgrading
doUpdate = NowUpdate()
LastVersion = sstrLocalVersion
End function
Rem ############################################### ################
http://bizhi.downcodes.com/
Rem ## Check whether it is the latest version
Rem ############################################### ################
Private function IsLastVersion()
Rem ## Initialize version information (initialize sarrVersionList array)
If iniVersionList Then
Rem ## If successful, compare versions
Dim i
IsLastVersion = True
For i = 0 to UBound(sarrVersionList)
If sarrVersionList(i) > sintLocalVersion Then
Rem ## If there is the latest version, exit the loop
IsLastVersion = False
Info = "Already the latest version!"
Exit For
End If
Next
Else
Rem ## Otherwise return error message
IsLastVersion = True
Info = "Error getting version information!(#2)"
End If
End function
Rem ############################################### ################
Rem ## Check whether it is the latest version
Rem ############################################### ################
Private function iniVersionList()
iniVersionList = False
Dim strVersion
strVersion = getVersionList()
Rem ## If the return value is empty, the initialization fails.
If strVersion = "" Then
Info = "Error......"
Exit function
End If
sstrVersionList = Replace(strVersion, " ", "")
sarrVersionList = Split(sstrVersionList, vbCrLf)
iniVersionList = True
End function
Rem ############################################### ################
Rem ## Check whether it is the latest version
Rem ############################################### ################
Private function getVersionList()
getVersionList = GetContent(UrlVersion)
End function
Rem ############################################### ################
Rem ## Start updating
Rem ############################################### ################
Private function NowUpdate()
Dim i
For i = UBound(sarrVersionList) to 0 step -1
Call doUpdateVersion(sarrVersionList(i))
Next
Info = "Upgrade completed! <a href=""" & sstrUrlLocal & UrlHistory & """>View</a>"
End function
Rem ############################################### ################
http://qqface.downcodes.com/
Rem ## Updated version content
Rem ############################################### ################
Private function doUpdateVersion(strVer)
doUpdateVersion = False
Dim intVer
intVer = toNum(Replace(strVer, ".", ""), 0)
Rem ## If the updated version is smaller than the current version, exit the update
If intVer <= sintLocalVersion Then
Exit function
End If
Dim strFileListContent, arrFileList, strUrlUpdate
strUrlUpdate = sstrUrlUpdate & intVer & FileType
strFileListContent = GetContent(strUrlUpdate)
If strFileListContent = "" Then
Exit function
End If
Rem ## Update the current version number
sintLocalVersion = intVer
sstrLocalVersion = strVer
Dim i, arrTmp
Rem ## Get update file list
arrFileList = Split(strFileListContent, vbCrLf)
Rem ## Update log
sstrLogContent = ""
sstrLogContent = sstrLogContent & strVer & ":" & vbCrLf
Rem ## Start updating
For i = 0 to UBound(arrFileList)
Rem ## Update format: version number/file.htm|destination file
arrTmp = Split(arrFileList(i), "|")
sstrLogContent = sstrLogContent & vbTab & arrTmp(1)
Call doUpdateFile(intVer & "/" & arrTmp(0), arrTmp(1))
Next
Rem ## Write to log file
sstrLogContent = sstrLogContent & Now() & vbCrLf
response.Write("<pre>" & sstrLogContent & "</pre>")
Call sDoCreateFile(Server.MapPath(sstrUrlLocal & "Log" & intVer & ".htm"), _
"<pre>" & sstrLogContent & "</pre>")
Call sDoAppendFile(Server.MapPath(sstrUrlLocal & UrlHistory), "<pre>" & _
strVer & "_______" & Now() & "</pre>" & vbCrLf)
End function
Rem ############################################### ################
Rem ## update file
Rem ############################################### ################
Private function doUpdateFile(strSourceFile, strTargetFile)
Dim strContent
strContent = GetContent(sstrUrlUpdate & strSourceFile)
Rem ## Update and write to the log
If sDoCreateFile(Server.MapPath(sstrUrlLocal & strTargetFile), strContent) Then
sstrLogContent = sstrLogContent & "Success" & vbCrLf
Else
sstrLogContent = sstrLogContent & "Failed" & vbCrLf
End If
End function
Rem ############################################### ################
Rem ## Get content remotely
Rem ############################################### ################
Private function GetContent(strUrl)
GetContent = ""
Dim oXhttp, strContent
Set oXhttp = Server.CreateObject("Microsoft.XMLHTTP")
'On Error Resume Next
With oXhttp
.Open "GET", strUrl, False, "", ""
.Send
If .readystate <> 4 Then Exit function
strContent = .Responsebody
strContent = sBytesToBstr(strContent)
End With
Set oXhttp = Nothing
If Err.Number <> 0 Then
response.Write(Err.Description)
Err.Clear
Exit function
End If
GetContent = strContent
End function
Rem ############################################### ################
Rem ############################################### ################
Rem ## Encoding conversion binary => string
Private function sBytesToBstr(vIn)
dimobjStream
set objStream = Server.CreateObject("adodb.stream")
objStream.Type = 1
objStream.Mode = 3
objStream.Open
objStream.Write vIn
objStream.Position = 0
objStream.Type = 2
objStream.Charset = "GB2312"
sBytesToBstr = objStream.ReadText
objStream.Close
set objStream = nothing
End function
Rem ############################################### ################
Rem ############################################### ################
Rem ## Encoding conversion binary => string
Private function sDoCreateFile(strFileName, ByRef strContent)
sDoCreateFile = False
Dim strPath
strPath = Left(strFileName, InstrRev(strFileName, "", -1, 1))
Rem ## Check the validity of path and file name
If Not(CreateDir(strPath)) Then Exit function
'If Not(CheckFileName(strFileName)) Then Exit function
'response.Write(strFileName)
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Dim fso, f
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile(strFileName, ForWriting, True)
f.Write strContent
f.Close
Set fso = nothing
Set f = nothing
sDoCreateFile = True
End function
Rem ############################################### ################
Rem ############################################### ################
Rem ## Encoding conversion binary => string
Private function sDoAppendFile(strFileName, ByRef strContent)
sDoAppendFile = False
Dim strPath
strPath = Left(strFileName, InstrRev(strFileName, "", -1, 1))
Rem ## Check the validity of path and file name
If Not(CreateDir(strPath)) Then Exit function
'If Not(CheckFileName(strFileName)) Then Exit function
'response.Write(strFileName)
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Dim fso, f
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile(strFileName, ForAppending, True)
f.Write strContent
f.Close
Set fso = nothing
Set f = nothing
sDoAppendFile = True
End function
Rem ############################################### ################
Rem ## Program to create a directory. If there are multiple levels of directories, create them one by one.
Rem ############################################### ################
Private function CreateDir(ByVal strLocalPath)
Dim i, strPath, objFolder, tmpPath, tmptPath
Dim arrPathList, intLevel
'On Error Resume Next
strPath = Replace(strLocalPath, "", "/")
Set objFolder = server.CreateObject("Scripting.FileSystemObject")
arrPathList = Split(strPath, "/")
intLevel = UBound(arrPathList)
For I = 0 To intLevel
If I = 0 Then
tmptPath = arrPathList(0) & "/"
Else
tmptPath = tmptPath & arrPathList(I) & "/"
End If
tmpPath = Left(tmptPath, Len(tmptPath) - 1)
If Not objFolder.FolderExists(tmpPath) Then objFolder.CreateFolder tmpPath
Next
Set objFolder = Nothing
If Err.Number <> 0 Then
CreateDir = False
Err.Clear
Else
CreateDir = True
End If
End function
Rem ############################################### ################
Rem ## long integer conversion
Rem ############################################### ################
Private function toNum(s, default)
If IsNumeric(s) and s <> "" then
toNum = CLng(s)
Else
toNum = default
End If
End function
Rem ############################################### ################
End Class
Rem ############################################### ####################################
%>