ASP program for automatic upgrade

Source: Internet
Author: User
Tags unpack web hosting

I also have a website that is also popular with web hosting. When I was a webmaster for nearly a year, I felt that my website Program Every upgrade is troublesome: Go to the official website to check the announcement, download the upgrade package to your local computer, decompress the package, and upload it to the VM through FTP. These are tiring and I am too lazy, so I think it would be nice to automatically upgrade the program. So I thought about it and wrote this article, hoping to help Web application developers. This is only for ASP, because I only use ASP
First, let's take a look at the traditional Win32 program upgrade process (such as anti-virus software). It relies on the Software Upgrade Program to connect to the server through the network for analysis and download the upgrade file to the local.
Web programs are a little different because they run on Web servers. In the end, it will overwrite the files on the upgraded server to the Web server. The webmaster's computer is just a transit. If you directly copy the files on the upgrade server to the Web server (instead of the webmaster transfer), the automatic upgrade is implemented.
Fortunately, the system comes with a Microsoft. XMLHTTP component used to access the Web. In ASP, you can call it to connect to the upgrade server and download the upgrade file.
The following code uses Microsoft. XMLHTTP to download an object:

<%
Set xpost = Createobject ("Microsoft. XMLHTTP ")
Xpost. Open "get", "http://www.0x54.org/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 ("the object has been downloaded! <Br> ")
%>

AboveCodeIt is just to save http://www.0x54.org/test.exeto the webserver's frontend directory. To learn more about microsoft.xmlhttp, please refer to msdn.
If there are many files, Microsoft. when XMLHTTP connects to the Network, some files that fail to be updated may fail to be updated. To avoid this problem, it is best to package all the files into a single file and download them to the Web before unpacking.
The package here is not a RAR or zip package, but defined by ourselves. For example, you can splice all the files into one and then separate them according to the special mark. It's not so troublesome now, because there is a ready-made method. We use the tailism to put all the files (in binary form) and their path information into the access database.
The following vbs file (from the ocean top 2006 plus) is used to package all the files in the current directory:

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 "the current directory has been packaged and the root directory is the current directory"
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

The following is the decompressed ASP file:

<%
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"), faster Rev (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
%>

well, with the above Code, it is not difficult to develop your own ASP Upgrade Program. The process is almost like this: Determine whether to upgrade (y) -> download the upgrade package-> unpack the upgrade package and overwrite the old file-> Delete the upgrade package-> Update version information-> OK
it's almost time to finish writing it here, some details such as version judgment are skipped.
I hope to use all kinds of web programs that will be automatically upgraded as soon as possible, so that I can stay idle. Haha.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.