<%
判断是否添加了文件
dim ulfilename, ulresult
ulsavetopath="/upload" 保存文件路径
ulmaxfilesize = 1000000 文件大小限制 1000,000
ulresult = douploadfile(ulmaxfilesize, ulfilename)
function douploadfile(maxuploadsize, filename)
dim upload, file, formname, formpath, fileext
filename = ""
set upload = new upload_yfly 建立上传对象
if isnull(upload.file) then
douploadfile= 6
exit function
end if
if upload.file.count = 0 then
douploadfile= 5 no file uploaded
else
set file = upload.file("ulfiletoupload")
if file.filename = "" then
douploadfile= 4 no file uploaded
else
上传文件不是空的时候处理上传
在目录后加(/)
formpath=upload.form("ulsavefilepath")
if right(formpath, 1) <> "/" then
formpath = formpath & "/"
end if
if file.filesize < 100 then
douploadfile= 1 文件大小太小
exit function
end if
if file.filesize > maxuploadsize then
douploadfile= 2 文件大小超过了限制
exit function
end if
fileext = lcase(right(file.filename, 4))
uploadsuc = false
if not (fileext = ".gif" or fileext = ".jpg") then
douploadfile= 3 文件格式不正确
exit function
end if
randomize
rannum = int(90000 * rnd) + 10000
文件名
filename = formpath & year(now) & month(now) & day(now) & hour(now) & minute(now) & second(now) & rannum & fileext
if file.filesize > 0 then 如果 filesize > 0 说明有文件数据
file.saveas server.mappath(filename) 保存文件
end if
douploadfile= 0
end if
set file = nothing
end if
set upload = nothing 删除此对象
end function
%>
<script runat="server" language="vbscript">
dim upfile_yfly_stream
class upload_yfly
dim form,file,version
private sub class_initialize
dim istart,ifilenamestart,ifilenameend,iend,vbenter,iformstart,iformend,thefile
dim strdiv,mformname,mformvalue,mfilename,mfilesize,mfilepath,idivlen,mstr
version=""
file = null
form = null
upfile_yfly_stream=null
if request.totalbytes<1 then exit sub
set form=createobject("scripting.dictionary")
set file=createobject("scripting.dictionary")
set upfile_yfly_stream=createobject("adodb.stream")
upfile_yfly_stream.mode=3
upfile_yfly_stream.type=1
upfile_yfly_stream.open
upfile_yfly_stream.write request.binaryread(request.totalbytes)
vbenter=chr(13)&chr(10)
idivlen=instring(1,vbenter)+1
strdiv=substring(1,idivlen)
iformstart=idivlen
iformend=instring(iformstart,strdiv)-1
while iformstart < iformend
istart=instring(iformstart,"name=""")
iend=instring(istart+6,"""")
mformname=substring(istart+6,iend-istart-6)
ifilenamestart=instring(iend+1,"filename=""")
if ifilenamestart>0 and ifilenamestart<iformend then
ifilenameend=instring(ifilenamestart+10,"""")
mfilename=substring(ifilenamestart+10,ifilenameend-ifilenamestart-10)
istart=instring(ifilenameend+1,vbenter&vbenter)
iend=instring(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.add mformname,thefile
else
istart=instring(iend+1,vbenter&vbenter)
iend=instring(istart+4,vbenter&strdiv)
if iend>istart then
mformvalue=substring(istart+4,iend-istart-4)
else
mformvalue=""
end if
form.add mformname,mformvalue
end if
iformstart=iformend+idivlen
iformend=instring(iformstart,strdiv)-1
wend
end sub
private function substring(thestart,thelen)
dim i,c,stemp
upfile_yfly_stream.position=thestart-1
stemp=""
for i=1 to thelen
if upfile_yfly_stream.eos then exit for
c=ascb(upfile_yfly_stream.read(1))
if c > 127 then
if upfile_yfly_stream.eos then exit for
stemp=stemp&chr(ascw(chrb(ascb(upfile_yfly_stream.read(1)))&chrb(c)))
i=i+1
else
stemp=stemp&chr(c)
end if
next
substring=stemp
end function
private function instring(thestart,varstr)
dim i,j,bt,thelen,str
instring=0
str=tobyte(varstr)
thelen=lenb(str)
for i=thestart to upfile_yfly_stream.size-thelen
if i>upfile_yfly_stream.size then exit function
upfile_yfly_stream.position=i-1
if ascb(upfile_yfly_stream.read(1))=ascb(midb(str,1)) then
instring=i
for j=2 to thelen
if upfile_yfly_stream.eos then
instring=0
exit for
end if
if ascb(upfile_yfly_stream.read(1))<>ascb(midb(str,j,1)) then
instring=0
exit for
end if
next
if instring<>0 then exit function
end if
next
end function
private sub class_terminate
if not isnull(form) then
form.removeall
set form=nothing
end if
if not isnull(file) then
file.removeall
set file=nothing
end if
if not isnull(upfile_yfly_stream) then
upfile_yfly_stream.close
set upfile_yfly_stream=nothing
end if
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 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
class fileinfo
dim formname,filename,filepath,filesize,filestart
private sub class_initialize
filename = ""
filepath = ""
filesize = 0
filestart= 0
formname = ""
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
upfile_yfly_stream.position=filestart-1
upfile_yfly_stream.copyto dr,filesize
dr.savetofile fullpath,2
dr.close
set dr=nothing
saveas=0
end function
end class
</script>
