<%
if session("admin")="" then
response.Redirect "login.asp"
response.End()
end if
%>
<!--#include file="Include/upfile_class.asp"-->
<%
on error resume next
%>

<%
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("../../pic")&"/"&current_M  '今月的目录
	folder_day=server.mappath("../../pic")&"/"&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
	Call DoSave()		' 存文件
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%>&style=<%=sStyleName%>" method=post name=myform enctype="multipart/form-data">
<input type=file name=uploadfile size=1 style="width:100%">
</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*100)
	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 Server.Mappath(sUploadDir & sSaveFileName)
	
	Set oFile = Nothing
	Set oUpload = Nothing	
	Call OutScript("parent.UploadSaved('"&current_M&"/"&current_D&"/"&sSaveFileName & "');parent.dialogArguments.addUploadFile('../../pic/"&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=LCase(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")))
	sStyleName = Trim(Request.QueryString("style"))
	sUploadDir="../../pic"&"/"&current_M&"/"&current_D
		If Right(sUploadDir, 1) <> "/" Then
			sUploadDir = sUploadDir & "/"
		End If

		Select Case sType
		Case "FILE"
			sAllowExt ="rar|doc|ppt|zip|xls"
			nAllowSize =102400
		Case "MEDIA"
			sAllowExt ="rm|rmvb|mov|wmv|mp3|wav|midi|jpeg"
			nAllowSize =102400
		Case "FLASH"
			sAllowExt ="swf"
			nAllowSize =102400
		Case Else
			sAllowExt ="gif|jpg|png|bmp"
			nAllowSize =102400
		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()
%>
