<script runat=server language=vbscript>
定义全局变量保存用户上传的内容
dim strmupload
自定义class处理上传文件
class fileupload
dim form,file
private sub class_initialize
dim istart,ifilenamestart,ifilenameend,iend,vbenter,iformstart,iformend,thefile
dim strdiv,mformname,mformvalue,mfilename,mfilesize,mfilepath,idivlen,mstr
if request.totalbytes<1 then exit sub
set form=createobject("scripting.dictionary")
set file=createobject("scripting.dictionary")
set strmupload=createobject("adodb.stream")
strmupload.mode=3
strmupload.type=1
strmupload.open
strmupload.write request.binaryread(request.totalbytes)
vbscript中的换行符字符串
vbenter=chr(13)&chr(10)
换行符的位置
idivlen=instrb(1,vbenter)+1
表单中不同控件数据的分隔字符串
strdiv=substrb(1,idivlen)
表单有效数据开始位置
iformstart=idivlen
表单有效数据结束位置
iformend=instrb(iformstart,strdiv)-1
循环得到表单中所有控件的值
while iformstart < iformend
得到控件的name
istart=instrb(iformstart,"name=""")
iend=instrb(istart+6,"""")
mformname=substrb(istart+6,iend-istart-6)
得到file控件的filename
ifilenamestart=instrb(iend+1,"filename=""")
如果是file控件
if ifilenamestart>0 and ifilenamestart<iformend then
ifilenameend=instrb(ifilenamestart+10,"""")
mfilename=substrb(ifilenamestart+10,ifilenameend-ifilenamestart-10)
istart=instrb(ifilenameend+1,vbenter&vbenter)
iend=instrb(istart+4,vbenter&strdiv)
如果上传了文件
if iend>istart then
得到上传文件的大小
mfilesize=iend-istart-4
else
mfilesize=0
end if
set thefile=new fileinfo
thefile.filename=getfilename(mfilename)
thefile.filepath=getfilepath(mfilename)
thefile.filesize=mfilesize
thefile.filestart=istart+4
thefile.formname=formname
将上传文件加入到file directory中
file.add mformname,thefile
如果不是file控件
else
istart=instrb(iend+1,vbenter&vbenter)
iend=instrb(istart+4,vbenter&strdiv)
if iend>istart then
mformvalue=substrb(istart+4,iend-istart-4)
else
mformvalue=""
end if
将控件的名、值加入到form directory中
form.add mformname,mformvalue
end if
准备读取下一个控件值
iformstart=iformend+idivlen
iformend=instrb(iformstart,strdiv)-1
wend
end sub
类终结
private sub class_terminate
form.removeall
file.removeall
set form=nothing
set file=nothing
strmupload.close
set strmupload=nothing
end sub
从全文件名中解析出路径
private function getfilepath(fullpath)
if fullpath <> "" then
getfilepath = left(fullpath,instrrev(fullpath, "\"))
else
getfilepath = ""
end if
end function
从全文件名中解析出短文件名
private function getfilename(fullpath)
if fullpath <> "" then
getfilename = mid(fullpath,instrrev(fullpath, "\")+1)
else
getfilename = ""
end if
end function
返回子串
private function substrb(thestart,thelen)
dim i,c,stemp
strmupload.position=thestart-1
stemp=""
for i=1 to thelen
if strmupload.eos then exit for
c=ascb(strmupload.read(1))
if c > 127 then
if strmupload.eos then exit for
stemp=stemp&chr(ascw(chrb(ascb(strmupload.read(1)))&chrb(c)))
i=i+1
else
stemp=stemp&chr(c)
end if
next
substrb=stemp
end function
返回指定字符串在strmupload中的位置
private function instrb(thestart,varstr)
dim i,j,bt,thelen,str
instrb=0
得到字节串
str=tobyte(varstr)
thelen=lenb(str)
for i=thestart to strmupload.size-thelen
if i>strmupload.size then exit function
strmupload.position=i-1
if ascb(strmupload.read(1))=ascb(midb(str,1)) then
instrb=i
for j=2 to thelen
if strmupload.eos then
instrb=0
exit for
end if
if ascb(strmupload.read(1))<>ascb(midb(str,j,1)) then
instrb=0
exit for
end if
next
if instrb<>0 then exit function
end if
next
end function
将字符串转换成字节
private function tobyte(str)
dim i,icode,c,ilow,ihigh
tobyte=""
for i=1 to len(str)
c=mid(str,i,1)
icode =asc(c)
if icode<0 then icode = icode + 65535
if icode>255 then
ilow = left(hex(asc(c)),2)
ihigh =right(hex(asc(c)),2)
tobyte = tobyte & chrb("&h"&ilow) & chrb("&h"&ihigh)
else
tobyte = tobyte & chrb(ascb(c))
end if
next
end function
end class
自定义类fileinfo
class fileinfo
dim formname,filename,filepath,filesize,filestart,dbcontent
类初始化
private sub class_initialize
filename = ""
filepath = ""
filesize = 0
filestart= 0
formname = ""
dbcontent = ""
end sub
自定义方法,将上传文件保存到服务器指定目录
public function saveas(fullpath)
dim dr,errorchar,i
saveas=1
if trim(fullpath)="" or filesize=0 or filestart=0 or filename="" then exit function
if filestart=0 or right(fullpath,1)="/" then exit function
set dr=createobject("adodb.stream")
dr.mode=3
dr.type=1
dr.open
strmupload.position=filestart-1
strmupload.copyto dr,filesize
dr.savetofile fullpath,2
dr.close
set dr=nothing
saveas=0
end function
自定义方法,将上传文件保存到数据库
public function save2db()
dim dr
if filesize=0 or filestart=0 or filename="" then exit function
strmupload.position=filestart-1
dbcontent = strmupload.read(filesize)
end function
end class
</script>
