<!--#include file="../admin_conn.asp"-->
<%
chkLogin
Server.ScriptTimeOut=999999
dim formName,formPath,filename,file_name,fileExt,Filesize,F_Type,formPath1
dim action:action = be("get","action")
dim id:id = be("get","id")
if isN(id) then id = "pic"

call upload()

sub upload()
	dim upload,file,ranNum,rename,filename1,errm,msg
	set upload=new UpFile_Class
	upload.GetDate(2048000)
	
	if upload.err > 0 then
	    select case upload.err
		case 1
			errm =  "请先选择你要上传的文件　[ <a onclick=history.go(-1)>重新上传</a> ]"
		case 2
			errm = "文件大小超过了限制　[ <a onclick=history.go(-1)>重新上传</a> ]"
		case else
			errm = "未知错误"
		end select
	else
		formPath= "../"&session("upfolder")&"/" & getSavePicPath() & "/"
		formPath = replace(formPath,"//","/")
		formPath = replace(formPath,chr(10),"")
		formPath = replace(formPath,chr(13),"")
		formPath1 = replace(formPath,"../../","")
		
		for each formName in upload.file
			set file=upload.file(formName)
			fileExt=lcase(file.FileExt)
			if lcase(fileEXT)="asp" and lcase(fileEXT)="asa" and lcase(fileEXT)="aspx" and lcase(fileEXT)="php" then
				CheckFileExt(fileEXT)=false
			end if
			if CheckFileExt(fileEXT)=false then
		 		errm= "文件格式不正确　[ <a href=### onclick=history.go(-1)>重新上传</a> ]"
		 		exit for
			end if
			
			randomize
			ranNum=int(90000*rnd)+10000
			file_name=year(now)&month(now)&day(now)&hour(now)&minute(now)&second(now)&ranNum
			filename=file_name&"."&fileExt
			rename=filename&"|"
			filename1=formPath&filename
			Filesize=file.FileSize
			if not objFile.IsFolderExists(formPath) then
				objFile.CreatePath formPath
			end if
			if Filesize>0 then
				file.SaveToFile Server.mappath(FileName1)
				if app_watermark =1 then 
					call ImgWriter(FileName1)
				end if
			end if
			set file=nothing
		next
	end if
	set upload=nothing
	if action="xht" then 
		writeA "{'err':'"&errm&"','msg':'"&app_installdir&formPath1&FileName&"'}"
	else
		if not isN(errm) then writeA errm else writeA "<script>parent.document.getElementById("""&id&""").value='"&formPath1&FileName&"'</script>" & "上传成功![ <a href=### onclick=history.go(-1)>重新上传</a> ]"
	end if
end sub

Private Function CheckFileExt (fileEXT)
	dim Forumupload,i
	Forumupload=split("wmv,avi,wma,mp3,mid,bmp,jpg,jpeg,gif,png,zip,rar,txt",",")
	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

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 
	If Err.number <> 0 Then
	response.write Err.Description
	response.end
	end if
End Function
Public Function FileDate
	oUpFileStream.Position = FileStart
	FileDate = oUpFileStream.Read (FileSize)
End Function
End Class
%>