<%
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 GetDate (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 
	end function
 
	'取得文件数据
	public function FileDate
		oUpFileStream.Position = FileStart
		FileDate = oUpFileStream.Read (FileSize)
	end function
end class
%>
