<!--#include file="Include/upfile_class.asp"-->
<!--#include file="../../inc/config.asp"-->
<!--#include file="../../inc/conn.asp"-->
<!-- #include file="../checklogin.asp" -->
<%
Server.ScriptTimeOut = 1800
dim fs,current_M,current_D,folder_mon,folder_day,current_time
dim s_year,s_month,s_day,s_hour
current_time=now
Set fs=Server.CreateObject("Scripting.FileSystemObject")
	current_M=chan_data(current_time)
	current_D=chan_day(current_time)
	folder_mon=server.mappath("../../uploadfiles")&"/"&current_M  '今月的目录
	folder_day=server.mappath("../../uploadfiles")&"/"&current_M&"/"&current_D  '今天的目录
	'创建本月文件夹
	If (fs.FolderExists(folder_mon)) Then		'今天的目录是否存在

	Else
		fs.CreateFolder(folder_mon)		'创建今天的目录
	End If
	'创建今天文件夹
	If (fs.FolderExists(folder_day)) Then		'今天的目录是否存在

	Else
		fs.CreateFolder(folder_day)		'创建今天的目录
	End If

set fs=nothing
Dim sType, sStyleName
Dim sAllowExt, nAllowSize, sUploadDir

Call InitUpload()		' 初始化上传变量

Dim sAction
sAction = UCase(Trim(Request.QueryString("action")))

Call ShowForm()			' 显示上传表单
If sAction = "SAVE" Then
	if(UpladParam(0)=true) then
		Call DoSave()		' 存文件
	else
		Call OutScript("parent.UploadError('暂停上传!')")
	end if
End If



Sub ShowForm() 
%>
<HTML>
<HEAD>
<TITLE>文件上传</TITLE>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<style type="text/css">
body, a, table, div, span, td, th, input, select{font:9pt;font-family: "宋体", Verdana, Arial, Helvetica, sans-serif;}
body {padding:0px;margin:0px}
</style>

<script language="JavaScript" src="dialog/dialog.js"></script>

</head>
<body bgcolor=menu>

<form action="?action=save&type=<%=sType%>" method=post name=myform enctype="multipart/form-data">
<input type=file name=uploadfile size=1 style="width:100%">
<input type="submit" value="asdf">
</form>

<script language=javascript>

var sAllowExt = "<%=sAllowExt%>";
// 检测上传表单
function CheckUploadForm() {
	if (!IsExt(document.myform.uploadfile.value,sAllowExt)){
		parent.UploadError("提示：\n\n请选择一个有效的文件，\n支持的格式有（"+sAllowExt+"）！");
		return false;
	}
	return true
}

// 提交事件加入检测表单
var oForm = document.myform ;
oForm.attachEvent("onsubmit", CheckUploadForm) ;
if (! oForm.submitUpload) oForm.submitUpload = new Array() ;
oForm.submitUpload[oForm.submitUpload.length] = CheckUploadForm ;
if (! oForm.originalSubmit) {
	oForm.originalSubmit = oForm.submit ;
	oForm.submit = function() {
		if (this.submitUpload) {
			for (var i = 0 ; i < this.submitUpload.length ; i++) {
				this.submitUpload[i]() ;
			}
		}
		this.originalSubmit() ;
	}
}

// 上传表单已装入完成
try {
	parent.UploadLoaded();
}
catch(e){
}

</script>

</body>
</html>
<% 
End Sub 

' 保存操作
Sub DoSave()
	Dim oUpload, oFile, sFileExt, sSourceFileName, sSaveFileName
	' 建立上传对象
	Set oUpload = New upfile_class
	' 取得上传数据,限制最大上传
	oUpload.GetData(nAllowSize*1024)
	If oUpload.Err > 0 Then
		Select Case oUpload.Err
		Case 1
			Call OutScript("parent.UploadError('请选择有效的上传文件！')")
		Case 2
			Call OutScript("parent.UploadError('你上传的文件总大小超出了最大限制（" & nAllowSize & "KB）！')")
		End Select
		Response.End
	End If

	Set oFile = oUpload.File("uploadfile")
	sFileExt = LCase(oFile.FileExt)
	sSourceFileName = oFile.FileName
	sFileExt=CheckValidExt(sFileExt)

	Dim sRnd
	Randomize
	sRnd = Int(900 * Rnd) + 100
	sSaveFileName = year(now) & month(now) & day(now) & hour(now) & minute(now) & second(now) & sRnd & "." & sFileExt
	oFile.SaveToFile (folder_day&"\" & sSaveFileName)	
	Set oFile = Nothing
	Set oUpload = Nothing
	'水印设置开始
	if(CanvasImg(0)=true) then
		On Error Resume Next
	    Set Jpeg = Server.CreateObject("Persits.Jpeg")
		if err=0 then 
		  	Set Jpeg = Server.CreateObject("Persits.Jpeg")
		  	Jpeg.Open server.MapPath("../../uploadfiles/"&current_M&"/"&current_D&"/"&sSaveFileName)
			Jpeg.Canvas.Font.Color =replace(CanvasImg(1),"#","&h") 
			Jpeg.Canvas.Font.Family=CanvasImg(2)
			Jpeg.Canvas.Font.Bold=CanvasImg(3)
			Jpeg.Canvas.Font.Size=CanvasImg(4)
			Jpeg.Canvas.Font.Quality=CanvasImg(5)
			Jpeg.Canvas.Print Jpeg.OriginalWidth*(1/10),Jpeg.OriginalHeight-Jpeg.Canvas.Font.Size,CanvasImg(6)   '水印文字
			Jpeg.Save server.MapPath("../../uploadfiles/"&current_M&"/"&current_D&"/"&sSaveFileName)
			Set Jpeg = Nothing
		else
          response.write("不能使用水印组件:Asp Jpeg")
		end if
	end if
	'水印设置结束
	Call OutScript("parent.UploadSaved('"&current_M&"/"&current_D&"/"&sSaveFileName & "');parent.dialogArguments.addUploadFile('../../uploadfiles/"&current_M&"/"&current_D&"', '" & sSaveFileName & "')")

End Sub

' 输出客户端脚本
Sub OutScript(str)
	Response.Write "<script language=javascript>" & str & ";history.back()</script>"
End Sub

' 检测扩展名的有效性
Function CheckValidExt(sExt)
	Dim b, i, aExt
	b = False
	aExt = Split(sAllowExt, "|")
	For i = 0 To UBound(aExt)
		If LCase(aExt(i)) = sExt Then
			b = True
			CheckValidExt=aExt(i)
			Exit For
		End If
	Next
	If b = False Then
		OutScript("parent.UploadError('提示：\n\n请选择一个有效的文件，\n支持的格式有（"+sAllowExt+"）！')")
		Response.End
	End If
End Function


' 初始化上传限制数据
Sub InitUpload()
	sType = UCase(Trim(Request.QueryString("type")))
	Select Case sType
		Case "FILE"
			sAllowExt=UpladParam(1)
			nAllowSize=UpladParam(2)
		Case "MEDIA"
			sAllowExt=UpladParam(3)
			nAllowSize=UpladParam(4)
		Case "FLASH"
			sAllowExt=UpladParam(5)
			nAllowSize=UpladParam(6)
		Case Else
			sAllowExt=UpladParam(7)
			nAllowSize=UpladParam(8)
	End Select	
	' 任何情况下都不允许上传asp脚本文件
	sAllowExt = Replace(UCase(sAllowExt), "ASP", "")
End Sub
'===================================================================
'                           时间转换函数
'===================================================================
function chan_file(shijian)
	s_year=year(shijian)
	if len(s_year)=2 then s_year="20"&s_year
	s_month=month(shijian)
	if s_month<10 then s_month="0"&s_month
	s_day=day(shijian)
	if s_day<10 then s_day="0"&s_day
	s_hour=hour(shijian)
	if s_hour<10 then s_hour="0"&s_hour
	s_minute=minute(shijian)
	if s_minute<10 then s_minute="0"&s_minute
	s_ss=second(shijian)
	if s_ss<10 then s_ss="0"&s_ss

	chan_file =s_year & s_month & s_day & s_hour & s_minute & s_ss
	
end function
'===================================================================
'                           时间转换函数2
'===================================================================
function chan_data(shijian)
	
	s_year=year(shijian)
	if len(s_year)=2 then s_year="20"&s_year
	s_month=month(shijian)
	if s_month<10 then s_month="0"&s_month
	s_day=day(shijian)
	if s_day<10 then s_day="0"&s_day
	s_hour=hour(shijian)
	chan_data =s_year & s_month 
	
end function
'===================================================================
'                           时间转换函数3
'===================================================================
function chan_day(shijian)	
	
	s_day=day(shijian)	
	chan_day=s_day 
	
end function

call connclose()
%>