| asp无组件文件上传的实例 1.库文件(upload.inc.asp)
 <%
 dim oupfilestream
 class upfile_class
 
 dim form,file,version,err
 
 private sub class_initialize
 version = "无组件上传类 version v1.0"
 err = -1
 end sub
 
 private sub class_terminate
 '清除变量及对像
 if err < 0 then
 form.removeall
 set form = nothing
 file.removeall
 set file = nothing
 oupfilestream.close
 set oupfilestream = nothing
 end if
 end sub
 
 public sub getdata (retsize)
 '定义变量
 dim requestbindate,sspace,bcrlf,sinfo,iinfostart,iinfoend,tstream,istart,ofileinfo
 dim ifilesize,sfilepath,sfiletype,sformvalue,sfilename
 dim ifindstart,ifindend
 dim iformstart,iformend,sformname
 '代码开始
 if request.totalbytes < 1 then
 err = 1
 exit sub
 end if
 if retsize > 0 then
 if request.totalbytes > retsize then
 err = 2
 exit sub
 end if
 end if
 set form = server.createobject ("scripting.dictionary")
 form.comparemode = 1
 set file = server.createobject ("scripting.dictionary")
 file.comparemode = 1
 set tstream = server.createobject ("adodb.stream")
 set oupfilestream = server.createobject ("adodb.stream")
 oupfilestream.type = 1
 oupfilestream.mode = 3
 oupfilestream.open
 oupfilestream.write request.binaryread (request.totalbytes)
 oupfilestream.position = 0
 requestbindate = oupfilestream.read
 iformend = oupfilestream.size
 bcrlf = chrb (13) & chrb (10)
 '取得每个项目之间的分隔符
 sspace = midb (requestbindate,1, instrb (1,requestbindate,bcrlf)-1)
 istart = lenb (sspace)
 iformstart = istart+2
 '分解项目
 do
 iinfoend = instrb (iformstart,requestbindate,bcrlf & bcrlf)+3
 tstream.type = 1
 tstream.mode = 3
 tstream.open
 oupfilestream.position = iformstart
 oupfilestream.copyto tstream,iinfoend-iformstart
 tstream.position = 0
 tstream.type = 2
 tstream.charset = "gb2312"
 sinfo = tstream.readtext
 '取得表单项目名称
 iformstart = instrb (iinfoend,requestbindate,sspace)-1
 ifindstart = instr (22,sinfo,"name=""",1)+6
 ifindend = instr (ifindstart,sinfo,"""",1)
 sformname = mid (sinfo,ifindstart,ifindend-ifindstart)
 '如果是文件
 if instr (45,sinfo,"filename=""",1) > 0 then
 set ofileinfo = new fileinfo_class
 '取得文件属性
 ifindstart = instr (ifindend,sinfo,"filename=""",1)+10
 ifindend = instr (ifindstart,sinfo,"""",1)
 sfilename = mid (sinfo,ifindstart,ifindend-ifindstart)
 ofileinfo.filename = mid (sfilename,instrrev (sfilename, "\")+1)
 ofileinfo.filepath = left (sfilename,instrrev (sfilename, "\")+1)
 ofileinfo.fileext = mid (sfilename,instrrev (sfilename, ".")+1)
 ifindstart = instr (ifindend,sinfo,"content-type: ",1)+14
 ifindend = instr (ifindstart,sinfo,vbcr)
 ofileinfo.filetype = mid (sinfo,ifindstart,ifindend-ifindstart)
 ofileinfo.filestart = iinfoend
 ofileinfo.filesize = iformstart -iinfoend -2
 ofileinfo.formname = sformname
 file.add sformname,ofileinfo
 else
 '如果是表单项目
 tstream.close
 tstream.type = 1
 tstream.mode = 3
 tstream.open
 oupfilestream.position = iinfoend
 oupfilestream.copyto tstream,iformstart-iinfoend-2
 tstream.position = 0
 tstream.type = 2
 tstream.charset = "gb2312"
 sformvalue = tstream.readtext
 if form.exists (sformname) then
 form (sformname) = form (sformname) & ", " & sformvalue
 else
 form.add sformname,sformvalue
 end if
 end if
 tstream.close
 iformstart = iformstart+istart+2
 '如果到文件尾了就退出
 loop until (iformstart+2) = iformend
 requestbindate = ""
 set tstream = nothing
 end sub
 end class
 
 '文件属性类
 class fileinfo_class
 dim formname,filename,filepath,filesize,filetype,filestart,fileext
 '保存文件方法
 public function savetofile (path)
 on error resume next
 dim ofilestream
 set ofilestream = createobject ("adodb.stream")
 ofilestream.type = 1
 ofilestream.mode = 3
 ofilestream.open
 oupfilestream.position = filestart
 oupfilestream.copyto ofilestream,filesize
 ofilestream.savetofile path,2
 ofilestream.close
 set ofilestream = nothing
 if err.number<>0 then
 savetofile=err.number&"**"&err.descripton
 else
 savetofile="ok"
 end if
 end function
 
 '取得文件数据
 public function filedate
 oupfilestream.position = filestart
 filedate = oupfilestream.read (filesize)
 end function
 end class
 %>
 
 
 
 2.处理用户提交后的页面(upload.asp)
 <!--#include file="upload.inc.asp"-->
 <html>
 <head>
 <title>文件上传</title>
 </head>
 <body topmargin="0" leftmargin="0">
 <table width=100% border=0 cellspacing="0" cellpadding="0"><tr><td class=tablebody1 width=100% height=100% >
 <%
 dim upload,file,formname,formpath,filename,fileext
 dim rannum
 call upfile()
 '===========无组件上传(upload_0)====================
 sub upfile()
 set upload=new upfile_class '建立上传对象
 upload.getdata (500*1024) '取得上传数据,此处即为500 k
 
 if upload.err > 0 then
 select case upload.err
 case 1
 response.write "请先选择你要上传的文件 [ <a href=# onclick=history.go(-1)>重新上传</a> ]"
 case 2
 response.write "图片大小超过了限制 500 k [ <a href=# onclick=history.go(-1)>重新上传</a> ]"
 end select
 exit sub
 else
 formpath=upload.form("filepath") '文件保存目录,此目录必须为程序可读写
 if formpath="" then
 formpath="rwdata/"
 end if
 '在目录后加(/)
 if right(formpath,1)<>"/" then
 formpath=formpath&"/"
 end if
 for each formname in upload.file '列出所有上传了的文件
 set file=upload.file(formname) '生成一个文件对象
 if file.filesize<100 then
 response.write "请先选择你要上传的图片 [ <a href=# onclick=history.go(-1)>重新上传</a> ]"
 response.end
 end if
 
 fileext=lcase(file.fileext)
 if checkfileext(fileext)=false then
 response.write "文件格式不正确 [ <a href=# onclick=history.go(-1)>重新上传</a> ]"
 response.end
 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 说明有文件数据
 result=file.savetofile(server.mappath(filename)) '保存文件
 if result="ok" then
 response.write formname&" upload ok, had saved to "&filename&"<br>"
 else
 response.write formname&" upload fail,"&result&"<br>"
 end if
 end if
 set file=nothing
 next
 set upload=nothing
 end if
 end sub
 
 '判断文件类型是否合格
 private function checkfileext (fileext)
 dim forumupload
 forumupload="gif,jpg,bmp,jpeg"
 forumupload=split(forumupload,",")
 for i=0 to ubound(forumupload)
 if lcase(fileext)=lcase(trim(forumupload(i))) then
 checkfileext=true
 exit function
 else
 checkfileext=false
 end if
 next
 end function
 %>
 </td></tr></table>
 </body>
 </html>
 
 
 
 3.html 表单(upload.html)
 <!doctype html public "-//w3c//dtd html 4.01 transitional//en">
 <html>
 <head>
 <meta http-equiv="content-type" content="text/html; charset=gb2312">
 <title>asp 无组件上传</title>
 </head>
 <body>
 请选择要上传的文件
 <form action="upfile.asp" method="post" enctype="multipart/form-data" name="form1">
 <input type="file" name="file">
 <!--<br>
 <input type="file" name="file">
 <br>
 <input type="file" name="file">
 <br>-->
 <input type="submit" name="submit" value="提交">
 </form>
 </body>
 </html>
 |