<%
文件名:updata.asp
远程地址
const url="http://localhost/test/"
action=request("action")
if action="updata" then
download(url&"config.txt")
download(url&"pack.jpg")
response.write("下载成功<a href=updata.asp?action=install>安装</a>")
elseif action="install" then
str=openfile("config.txt")
if str="" then
response.write "缺少本地配置文件config.txt"
else
size=regexptest("size",str)
call install("pack.jpg",size)
end if
else
str=getpage(url&"config.txt")
if str="" then
response.write "不存在可用更新或者本地配置不正确"
response.end
end if
str1=openfile("config.txt")
if str1="" then
response.write "缺少本地配置文件config.txt无法获知本地程序的安装时间"
response.end
end if
updatatime=regexptest("time",str)
updatatime1=regexptest("time",str1)
if datediff("d",updatatime1,updatatime)>0 then
response.write("存在可用更新,更新日期:"&updatatime&"<a href=updata.asp?action=updata>下载</a>")
else
response.write "您的程序是最新的了"
end if
end if
function openfile(filename)
set fso=server.createobject("scripting.filesystemobject")
if fso.fileexists(server.mappath(filename)) then
set f1=fso.opentextfile(server.mappath(filename),1,true)
openfile=f1.readall
f1.close
else
openfile=""
end if
set fso=nothing
end function
function getpage(url)
set xmlhttp=server.createobject("microsoft.xmlhttp")
xmlhttp.open "get",url,false
xmlhttp.send
if xmlhttp.status<>200 then
getpage=""
else
getpage=bytes2bstr(xmlhttp.responsebody)
end if
end function
function bytes2bstr(vin)
dim strreturn
dim i,thischarcode,nextcharcode
strreturn = ""
for i = 1 to lenb(vin)
thischarcode = ascb(midb(vin,i,1))
if thischarcode < &h80 then
strreturn = strreturn & chr(thischarcode)
else
nextcharcode = ascb(midb(vin,i+1,1))
strreturn = strreturn & chr(clng(thischarcode) * &h100 + cint(nextcharcode))
i = i + 1
end if
next
bytes2bstr = strreturn
end function
function regexptest(patrn,strng)
dim regex,match,matches建立变量。
set regex = new regexp建立正则表达式。
regex.pattern = patrn&"=(.+?)\n"设置模式。
regex.ignorecase = true设置是否区分字符大小写。
regex.global = true设置全局可用性。
set matches = regex.execute(strng)执行搜索。
for each match in matches遍历匹配集合。
retstr = match.value
next
regexptest = replace(retstr,patrn&"=","")
end function
function download(url)
temp=split(url,"/")
filename=temp(ubound(temp))
set xmlhttp=server.createobject("microsoft.xmlhttp")
xmlhttp.open "get",url,false
xmlhttp.send
if xmlhttp.status<>200 then
download=""
else
set fso=server.createobject("scripting.filesystemobject")
if fso.fileexists(server.mappath(filename)) then
fso.deletefile(server.mappath(filename))
end if
set fso=nothing
img=xmlhttp.responsebody
set objadostream=server.createobject("adodb.stream")
objadostream.open
objadostream.type=1
objadostream.write(img)
objadostream.savetofile(server.mappath(filename))
objadostream.seteos
set objadostream=nothing
download=filename
end if
set xmlhttp=nothing
end function
function install(filename,size)
on error resume next
path=server.mappath("./")
set fso=server.createobject("scripting.filesystemobject")
set s=server.createobject("adodb.stream")
set s1=server.createobject("adodb.stream")
set s2=server.createobject("adodb.stream")
s.open
s1.open
s2.open
s.type=1
s1.type=1
s2.type=1
s.loadfromfile(server.mappath(filename))
s.position=size
s1.write(s.read)
s1.position=0
s1.type=2
s1.charset="gb2312"
s1.position=0
a=split(s1.readtext,vbcrlf)
s.position=0
i=0
while(i<ubound(a))
b=split(a(i),">")
if b(0)="folder" then
if not fso.folderexists(path&b(2)) then
fso.createfolder(path&b(2))
end if
elseif b(0)="file" then
if fso.fileexists(path&b(2)) then
fso.deletefile(path&b(2))
end if
s2.position=0
s2.write(s.read(b(1)))
s2.seteos
s2.savetofile(path&b(2))
end if
i=i+1
wend
s.close
s1.close
s2.close
set s=nothing
set s1=nothing
set s2=nothing
set fso=nothing
if err.number<>0 then
response.write err.description
else
response.write "安装成功"
end if
end function
%>
——————————————————————————–
<%
文件名称:pack.asp
on error resume next
set fso=server.createobject("scripting.filesystemobject")
if fso.fileexists(server.mappath("./pack.jpg")) then
response.write("pack.jpg已经存在")
response.end()
end if
dim str,s,s1,s2
set s=server.createobject("adodb.stream")
set s1=server.createobject("adodb.stream")
set s2=server.createobject("adodb.stream")
s.open
s1.open
s2.open
s.type=1
s1.type=1
s2.type=2
call writefile(server.mappath("./"))
s2.charset="gb2312"
s2.writetext(str)
s2.position=0
s2.type=1
s2.position=0
bin=s2.read
s2.position=0
s2.type=2
s2.writetext("time="&now&vbcrlf)
s2.writetext("size="&s1.size&vbcrlf)
s2.writetext("run="&request.form("run")&vbcrlf)
s2.seteos
s2.savetofile(server.mappath("./config.txt"))
s1.write(bin)
s1.seteos
s1.savetofile(server.mappath("./pack.jpg"))
s.close
s1.close
s2.close
set s=nothing
set s1=nothing
set s2=nothing
if err.number<>0 then
response.write err.description
else
response.write("完成")
end if
function writefile(folderspec)
set fso = createobject("scripting.filesystemobject")
set f = fso.getfolder(folderspec)
set fc = f.files
for each f1 in fc
if f1.name<>"pack.asp" then
str=str&"file>"&f1.size&">"&replace(folderspec&"\"&f1.name,server.mappath("./"),"")&vbcrlf
s.loadfromfile(folderspec&"\"&f1.name)
img=s.read()
s1.write(img)
end if
next
set fc = f.subfolders
for each f1 in fc
str=str&"folder>0>"&replace(folderspec&"\"&f1.name,server.mappath("./"),"")&vbcrlf
writefile(folderspec&"\"&f1.name)
next
set fso=nothing
end function
%>
——————————————————————————–
asp升级程序使用说明
本程序分两部分:
1、asp文件打包程序pack.asp
把这个程序和要打包的程序放到一个目录下,然后运行pack.asp,得到pack.jpg和config.txt
2、asp在线更新、下载、安装程序updata.asp
这个程序可以用来检查是否存在可用更新,和updata.asp同一目录要存在上面得到的config.txt,因为config里面有当前程序的安装日期,用来和网上的程序比较用的。
使用前,先修改updata.asp里的url变量的值,使其等于你存放升级程序的url,运行updata.asp就可查看是否存在可用更新,如果存在就可用按着向导一步一步下载并安装更新了。
远程地址url下面存放用pack.asp得到的pack.jpg和config.txt
本程序既可以用来做升级程序,当然如果原来安装目录下是空的,那就是一个完整的安装程序,^_^,也可以把updata.asp放到后台的首页里,这样每次登陆都可以自动检查是否有可用更新
注意:本地或者远程没有config.txt会导致程序不可用,以后会考虑加入这个容错机制。