asp不用组件实现文件上传

FAQ里面有许多关于无组件上传的帖子.比如:

  1<script language="VBSCRIPT" runat="SERVER">   
  2'----------------------------------------------------------------------   
  3'******************* 无组件上传类 ********************************   
  4'修改者:梁无惧   
  5'电子邮件:[email protected]   
  6'网站:http://asp.25cn.com   
  7'原作者:稻香老农   
  8'原作者网站:http://www.5xsoft.com   
  9'声明:此上传类是在化境编程界发布的无组件上传类的基础上修改的.   
 10'在与化境编程界无组件上传类相比,速度快了将近50倍,当上传4M大小的文件时   
 11'服务器只需要10秒就可以处理完,是目前最快的无组件上传程序,当前版本为0.93   
 12'源代码公开,免费使用,对于商业用途,请与作者联系   
 13'**********************************************************************   
 14'----------------------------------------------------------------------   
 15dim oUpFileStream   
 16  
 17Class upload_file   
 18  
 19dim Form,File,Version   
 20  
 21Private Sub Class_Initialize   
 22dim RequestBinDate,sStart,bCrLf,sInfo,iInfoStart,iInfoEnd,tStream,iStart,oFileInfo   
 23dim iFileSize,sFilePath,sFileType,sFormValue,sFileName   
 24dim iFindStart,iFindEnd   
 25dim iFormStart,iFormEnd,sFormName   
 26Version="无组件上传类 Version 0.93"   
 27set Form=Server.CreateObject("Scripting.Dictionary")   
 28set File=Server.CreateObject("Scripting.Dictionary")   
 29if Request.TotalBytes<1 then Exit Sub   
 30set tStream = Server.CreateObject("adodb.stream")   
 31set oUpFileStream = Server.CreateObject("adodb.stream")   
 32oUpFileStream.Type = 1   
 33oUpFileStream.Mode =3   
 34oUpFileStream.Open   
 35oUpFileStream.Write Request.BinaryRead(Request.TotalBytes)   
 36oUpFileStream.Position=0   
 37RequestBinDate =oUpFileStream.Read   
 38iFormStart = 1   
 39iFormEnd = LenB(RequestBinDate)   
 40bCrLf = chrB(13) & chrB(10)   
 41sStart = MidB(RequestBinDate,1, InStrB(iFormStart,RequestBinDate,bCrLf)-1)   
 42iStart = LenB (sStart)   
 43iFormStart=iFormStart+iStart+1   
 44while (iFormStart + 10) < iFormEnd   
 45iInfoEnd = InStrB(iFormStart,RequestBinDate,bCrLf & bCrLf)+3   
 46tStream.Type = 1   
 47tStream.Mode =3   
 48tStream.Open   
 49oUpFileStream.Position = iFormStart   
 50oUpFileStream.CopyTo tStream,iInfoEnd-iFormStart   
 51tStream.Position = 0   
 52tStream.Type = 2   
 53tStream.Charset ="gb2312"   
 54sInfo = tStream.ReadText   
 55'取得表单项目名称   
 56iFormStart = InStrB(iInfoEnd,RequestBinDate,sStart)   
 57iFindStart = InStr(22,sInfo,"name=""",1)+6   
 58iFindEnd = InStr(iFindStart,sInfo,"""",1)   
 59sFormName = Mid (sinfo,iFindStart,iFindEnd-iFindStart)   
 60'如果是文件   
 61if InStr (45,sInfo,"filename=""",1) > 0 then   
 62set oFileInfo=new FileInfo   
 63'取得文件名   
 64iFindStart = InStr(iFindEnd,sInfo,"filename=""",1)+10   
 65iFindEnd = InStr(iFindStart,sInfo,"""",1)   
 66sFileName = Mid (sinfo,iFindStart,iFindEnd-iFindStart)   
 67  
 68oFileInfo.FileName=getFileName(sFileName)   
 69oFileInfo.FilePath=getFilePath(sFileName)   
 70'取得文件类型   
 71iFindStart = InStr(iFindEnd,sInfo,"Content-Type: ",1)+14   
 72iFindEnd = InStr(iFindStart,sInfo,vbCr)   
 73oFileInfo.FileType =Mid (sinfo,iFindStart,iFindEnd-iFindStart)   
 74oFileInfo.FileStart =iInfoEnd   
 75oFileInfo.FileSize = iFormStart -iInfoEnd -3   
 76oFileInfo.FormName=sFormName   
 77file.add sFormName,oFileInfo   
 78else   
 79'如果是表单项目   
 80tStream.Close   
 81tStream.Type =1   
 82tStream.Mode =3   
 83tStream.Open   
 84oUpFileStream.Position = iInfoEnd   
 85oUpFileStream.CopyTo tStream,iFormStart-iInfoEnd-3   
 86tStream.Position = 0   
 87tStream.Type = 2   
 88tStream.Charset ="gb2312"   
 89sFormValue = tStream.ReadText   
 90form.Add sFormName,sFormValue   
 91end if   
 92tStream.Close   
 93iFormStart=iFormStart+iStart+1   
 94wend   
 95RequestBinDate=""   
 96set tStream =nothing   
 97End Sub   
 98  
 99Private Sub Class_Terminate   
100if not Request.TotalBytes<1 then   
101form.RemoveAll   
102file.RemoveAll   
103set form=nothing   
104set file=nothing   
105oUpFileStream.Close   
106set oUpFileStream =nothing   
107end if   
108End Sub   
109  
110  
111Private function GetFilePath(FullPath)   
112If FullPath <> "" Then   
113GetFilePath = left(FullPath,InStrRev(FullPath, "\"))   
114Else   
115GetFilePath = ""   
116End If   
117End function   
118  
119Private function GetFileName(FullPath)   
120If FullPath <> "" Then   
121GetFileName = mid(FullPath,InStrRev(FullPath, "\")+1)   
122Else   
123GetFileName = ""   
124End If   
125End function   
126  
127End Class   
128  
129Class FileInfo   
130dim FormName,FileName,FilePath,FileSize,FileType,FileStart   
131Private Sub Class_Initialize   
132FileName = ""   
133FilePath = ""   
134FileSize = 0   
135FileStart= 0   
136FormName = ""   
137FileType = ""   
138End Sub   
139  
140Public function SaveToFile(FullPath)   
141dim oFileStream,ErrorChar,i   
142SaveToFile=1   
143if trim(fullpath)="" or right(fullpath,1)="/" then exit function   
144set oFileStream=CreateObject("Adodb.Stream")   
145oFileStream.Type=1   
146oFileStream.Mode=3   
147oFileStream.Open   
148oUpFileStream.position=FileStart   
149oUpFileStream.copyto oFileStream,FileSize   
150oFileStream.SaveToFile FullPath,2   
151oFileStream.Close   
152set oFileStream=nothing   
153SaveToFile=0   
154end function   
155End Class   
156</script>
Published At
Categories with Web编程
Tagged with
comments powered by Disqus