<%
dim Data_Netout

Class Netout
dim objForm,objFile,Version

Private Sub Class_Initialize 
	dim RequestData,sStart,vbCrlf,sInfo,iInfoStart,iInfoEnd,tStream,iStart,theFile
	dim iFileSize,sFilePath,sFileType,sFormValue,sFileName
	dim iFindStart,iFindEnd
	dim iFormStart,iFormEnd,sFormName
	set objForm=Server.CreateObject("Scripting.Dictionary")
	set objFile=Server.CreateObject("Scripting.Dictionary")
	if Request.TotalBytes<1 then Exit Sub
	
	set tStream = Server.CreateObject("adodb.stream")
	set Data_Netout = Server.CreateObject("adodb.stream")
	Data_Netout.Type = 1
	Data_Netout.Mode =3
	Data_Netout.Open
	Data_Netout.Write  Request.BinaryRead(Request.TotalBytes)
	Data_Netout.Position=0
	RequestData =Data_Netout.Read 
	
	iFormStart = 1
	iFormEnd = LenB(RequestData)
	vbCrlf = chrB(13) & chrB(10)
	sStart = MidB(RequestData,1, InStrB(iFormStart,RequestData,vbCrlf)-1)
	iStart = LenB (sStart)
	iFormStart=iFormStart+iStart+1
	
	while (iFormStart + 10) < iFormEnd 
		iInfoEnd = InStrB(iFormStart,RequestData,vbCrlf & vbCrlf)+3
		tStream.Type = 1
		tStream.Mode =3
		tStream.Open
		Data_Netout.Position = iFormStart
		Data_Netout.CopyTo tStream,iInfoEnd-iFormStart
		tStream.Position = 0
		tStream.Type = 2
		tStream.Charset ="gb2312"
		sInfo = tStream.ReadText
		tStream.Close
		'取得表单项目名称
		iFormStart = InStrB(iInfoEnd,RequestData,sStart)
		iFindStart = InStr(22,sInfo,"name=""",1)+6
		iFindEnd = InStr(iFindStart,sInfo,"""",1)
		sFormName = lcase(Mid (sinfo,iFindStart,iFindEnd-iFindStart))
		'如果是文件
		if InStr (45,sInfo,"filename=""",1) > 0 then
			'取得文件信息
			set theFile=new FileInfo
			iFindStart = InStr(iFindEnd,sInfo,"filename=""",1)+10
			iFindEnd = InStr(iFindStart,sInfo,"""",1)
			sFileName = Mid (sinfo,iFindStart,iFindEnd-iFindStart)
			'取得文件名
			theFile.FileName=getFileName(sFileName)
			'取得文件路径
			theFile.FilePath=getFilePath(sFileName)
			'取得文件类型
			iFindStart = InStr(iFindEnd,sInfo,"Content-Type: ",1)+14
			iFindEnd = InStr(iFindStart,sInfo,vbCr)
			theFile.FileStart =iInfoEnd
			'取得文件大小
			theFile.FileSize = iFormStart -iInfoEnd -3
			theFile.FormName=sFormName
			if not objFile.Exists(sFormName) then
				objFile.add sFormName,theFile
			end if
		else
			'如果是表单项目
			tStream.Type =1
			tStream.Mode =3
			tStream.Open
			Data_Netout.Position = iInfoEnd 
			Data_Netout.CopyTo tStream,iFormStart-iInfoEnd-3
			tStream.Position = 0
			tStream.Type = 2
			tStream.Charset ="gb2312"
			sFormValue = tStream.ReadText 
			tStream.Close
			if objForm.Exists(sFormName) then
				objForm(sFormName)=objForm(sFormName)&", "&sFormValue		  
			else
				objForm.Add sFormName,sFormValue
			end if
		end if
		iFormStart=iFormStart+iStart+1
	wend
	RequestData=""
	set tStream =nothing
End Sub

Private Sub Class_Terminate  
	if Request.TotalBytes>0 then
		objForm.RemoveAll
		objFile.RemoveAll
		set objForm=nothing
		set objFile=nothing
		Data_Netout.Close
		set Data_Netout =nothing
	end if
End Sub
 
Public function Form(strForm)
	strForm=lcase(strForm)
	if not objForm.exists(strForm) then
		Form=""
	else
		Form=objForm(strForm)
	end if
end function

Public function File(strFile)
	strFile=lcase(strFile)
	if not objFile.exists(strFile) then
		set File=new FileInfo
	else
		set File=objFile(strFile)
	end if
end function

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
 
End Class

'########################
Function ConvertEXIFDate(ExifDate)
'########################
p=Instr(ExifDate," ")
If p>0 AND Instr(ExifDate,":")>0 Then
	d=Left(ExifDate,p-1)
	t=Mid(ExifDate,p+1)
	a1=Split(d,":")
	a2=Split(t,":")
	ConvertEXIFDate=DateSerial(a1(0),a1(1),a1(2)) & " " & TimeSerial(a2(0),a2(1),a2(2)) 
	ConvertEXIFDate=ToDBDate(ConvertEXIFDate)
Else
	ConvertEXIFDate=ExifDate
End If
End Function

'########################
Function ToDBDate(sDate)
'########################
s=sDate
If IsDate(s) Then s=Year(s) & TwoDigits(Month(s)) & TwoDigits(Day(s)) & TwoDigits(Hour(s)) & TwoDigits(Minute(s)) & TwoDigits(Second(s))
ToDBDate=s
End Function

'########################
Function TwoDigits(Str)
'########################
If Len(Str)<2 Then TwoDigits="0" & Str Else TwoDigits=Str
End Function

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=true
	if trim(fullpath)="" or FileStart=0 or FileName="" or right(fullpath,1)="/" then exit function
	set dr=CreateObject("Adodb.Stream")
	dr.Mode=3
	dr.Type=1
	dr.Open
	Data_Netout.position=FileStart
	Data_Netout.copyto dr,FileSize
	dr.SaveToFile FullPath,2
	dr.Close
	set dr=nothing 
	SaveAs=false
end function
End Class
%>
<%
'########################
Function ObjectExists(oClass)
'########################
	On Error Resume Next
	Set obj=Server.CreateObject(oClass)
	If Err Then ObjectExists=False Else ObjectExists=True
	Set Obj=Nothing
End Function
%>
<!--#include File="conn.asp"-->
<!--#include File="exif.asp"-->
<html>
<head>
<title>文件上传</title>
<link href="images/bbs.css" rel="stylesheet" type="text/css">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<style type="text/css">
<!--
.style1 {color: #F2F8FF}
-->
</style>
</head>
<body>
<table width="365" border="0" cellspacing="0" cellpadding="0" align="center" bordercolordark="#CCCCCC" bordercolorlight="#000000">
  <tr>
    <td align="center">
	<table width="75%" border="0" cellspacing="0" cellpadding="0">
        <tr>
          <td height="48" align="center"><strong>图片上传信息</strong></td>
        </tr>
      </table>
      <table width="50%" border="0" cellspacing="0" cellpadding="0">
        <tr>
          <td align="center">
<%
dim UpImages,File,formName,formPath,iCount
dim gFilePath,gPath,gRootUrl
dim ImageInfo(6),Content(6),FilePath(6)
set UpImages=new Netout ''建立上传对象

ClassID=Clng(UpImages.form("ClassID"))
if ClassID=0 then
	response.Redirect("add.asp")
end if

Photoer=trim(UpImages.form("Photoer"))
titleName=trim(UpImages.form("titleName"))
desc=trim(UpImages.form("desc"))
Recomm=UpImages.form("Recomm")
'------------人物介绍----------------
tblxinbei1=trim(UpImages.form("xinbei"))
tblcsd1=trim(UpImages.form("csd"))
tblxz1=trim(UpImages.form("xz"))
tblsg1=trim(UpImages.form("sg"))
tbltz1=trim(UpImages.form("tz"))
tblxx1=trim(UpImages.form("xx"))
tblxl1=trim(UpImages.form("xl"))
tblcolor1=trim(UpImages.form("color"))
tbltc1=trim(UpImages.form("tc"))
tblzp1=trim(UpImages.form("zp"))
'-------------------------------
for i=1 to 5
	Content(i)=trim(UpImages.form("content"&i))
	ImageInfo(i)=trim(UpImages.form("width"&i))&","&trim(UpImages.form("height"&i))&","&trim(UpImages.form("size"&i))
next

formPath="UploadImages/"

iCount=0

for each formName in UpImages.objFile ''列出所有上传了的文件
	set File=UpImages.file(formName)  ''生成一个文件对象
	if File.FileSize>0 and instr("jpg,gif,bmp,png",Lcase(right(File.FileName,3)))>0 then         ''如果 FileSize > 0 说明有文件数据
		randomize
		ranNum=int(900*rnd)+100
		FileName=formPath&year(now)&month(now)&day(now)&hour(now)&minute(now)&second(now)&ranNum&"."&right(File.FileName,3)
		File.SaveAs Server.mappath(FileName)   ''保存文件
		iCount=iCount+1
		FilePath(iCount)=FileName
	end if
	set File=nothing
next

if iCount>0 then
	set rs=server.CreateObject("adodb.recordset")
	rs.open "select * from Title where ClassID="&ClassID&" and Photoer='"&Photoer&"' and titleName='"&titleName&"'",conn,1,3
	if (not rs.eof) and (not rs.bof) then
		rs("titleCount")=rs("titleCount")+iCount
		rs.update
	else
		rs.addnew
		rs("ClassID")=ClassID
		rs("titleCount")=iCount
		set Num=server.CreateObject("adodb.recordset")
		Num.open "select max(titleNum) from Title",conn,1,1
		if Num.eof or Num.bof or isNull(Num(0)) then
			titleNum=10000001
		else
			titleNum=Num(0)+1
		end if
		Num.Close
		set Num=nothing
		rs("titleNum")=titleNum
		rs("titleName")=titleName
		rs("Photoer")=Photoer
		rs("desc")=desc
		rs("Recomm")=Recomm
		rs("updatetime")=now
		rs("tblName")=titleName
		RS("tblxinbei")=tblxinbei1
		RS("tblcsd")=tblcsd1
		RS("tblxz")=tblxz1
		RS("tblsg")=tblsg1
		RS("tbltz")=tbltz1
		RS("tblxx")=tblxx1
		RS("tblxl")=tblxl1
		RS("tblcolor")=tblcolor1
		RS("tbltc")=tbltc1
		RS("tblzp")=tblzp1
		rs.update
	end if
	RsClose

	set tID=conn.execute("select * from title where ClassID="&ClassID&" and Photoer='"&Photoer&"' and titleName='"&titleName&"'")

	set rs=server.CreateObject("adodb.recordset")
	sql="select * from imgbook"
	rs.open sql,conn,1,3
	
	for i=1 to iCount
		if (not isempty(FilePath(i))) or FilePath(i)<>"" then
			rs.addnew
			rs("titleID")=tID(0)
			set Num=server.CreateObject("adodb.recordset")
			Num.open "select max(Num) from imgbook",conn,1,1
			if Num.eof or Num.bof or isNull(Num(0)) then
				ImageNum=10000001
			else
				ImageNum=Num(0)+1
			end if
			Num.Close
			set Num=nothing
			'--------------------EXIF-------------------------------
        GetExifInfo=GetImageExifInfo(FilePath(i))
'----------------------------------aspjpg缩图组件--------------------------------
If ObjectExists("Persits.Jpeg") Then
s_filepath=Replace(FilePath(i),"UploadImages","UploadImages/s")
' Create instance of AspJpeg
Set Jpeg = Server.CreateObject("Persits.Jpeg")
' Compute path to source image
Path = Server.MapPath(FilePath(i))

' Open source image
Jpeg.Open Path

' Decrease image size by 50%
if Jpeg.Width>Jpeg.Height then
beisu=Jpeg.Width/120
Jpeg.Width = Jpeg.OriginalWidth / beisu
Jpeg.Height = Jpeg.OriginalHeight / beisu
Else
beisu=Jpeg.Height/150
Jpeg.Width = Jpeg.OriginalWidth / beisu
Jpeg.Height = Jpeg.OriginalHeight / beisu
End if
' create thumbnail and save it to disk
Jpeg.Save Server.MapPath(s_filepath)
End if

'----------------------------------aspjpg水印--------------------------------
If ObjectExists("Persits.Jpeg") Then
 ' 读取要处理的原文件 
 Jpeg.Open Trim(Server.MapPath(FilePath(i))) 
 ' Open a logo file 
 Set Logobox = Server.CreateObject("Persits.Jpeg") 
 LogoPath = Server.MapPath("..\images") & "\shuiyin.gif"
 Logobox.Open LogoPath 
 ' resize logo image, preserve aspect ratio（重新设置图片的大小） 
 Logobox.Width = 180
 Logobox.Height = Logobox.Width * Logobox.OriginalHeight / Logobox.OriginalWidth
   Jpeg.Canvas.Pen.Color  = &H000000  '// 边框的颜色
    Jpeg.Canvas.Pen.Width  = 1    '// 边框的粗细
    Jpeg.Canvas.Brush.Solid = False   '// 图片边框内是否填充颜色
    Jpeg.DrawImage Jpeg.Width-210, Jpeg.Height-94, Logobox  ,0.6,&HFFFFFF
    Jpeg.Canvas.Bar 0, 0, Jpeg.Width, Jpeg.Height '// 图片边框线的位置坐标和大小
    Jpeg.Save Server.MapPath(FilePath(i))  '// 生成文件
	Set Jpeg = Nothing
	Set Logobox = Nothing
End if

'--------------------------------------------------------------------
			'------------------------------------------------------
			rs("Num")=ImageNum
			rs("content")=Content(i)
			rs("photo")=FilePath(i)
			rs("exifinfo")=Trim(GetExifInfo)
			if ImageInfo(i)<>"宽,高,大小(K)" then
				rs("ImageInfo")=ImageInfo(i)
			end if
			rs.update
		end if
	next
	rsclose
	tID.Close
	set tID=nothing
end if

Msg=Msg&"总共上传<font color=red>"&iCount&"</font>张图片。"
set UpImages=nothing  ''删除此对象

ShowMsg Msg

sub ShowMsg(Msg)
	set UpImages=nothing
	response.write Msg
end sub
ConnClose
%>
          </td>
        </tr>
      </table> 
      
      <table width="50%" height="40" border="0" cellpadding="0" cellspacing="0">
        <tr>
          <td align="center"><br><br><strong>还需要继续上传吗？</strong><br><Br><a href='javascript:history.back(-1);'>确定</a><span class="7 style1">阿熊</span><a href='manage.asp' class='black'>取消</a></td>
        </tr>
      </table>
    </td>
  </tr>
</table>
</body>
</html>