<!--#include file="conn.asp"-->
<!--#include file="inc/upfile_class.asp"-->
<%
Server.ScriptTimeOut = 1800
Dim sAllowExt, nAllowSize, sUploadDir
dim themax

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">
<Link rel="stylesheet" type="text/css" href="pop.css">
<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>
</head>
<body bgcolor=menu>

<form action="?action=save" method=post name=myform enctype="multipart/form-data">
<input type=file name=uploadfile size=1 style="width:100%">
<%response.Write "可上传的文件："&sAllowExt&"<br>单个文件大小："&nAllowSize&"KB　可用空间："&themax&"KB"%>
</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){
}

// 是否有效的扩展名
function IsExt(url, opt){
	var sTemp;
	var b=false;
	var s=opt.toUpperCase().split("|");
	for (var i=0;i<s.length ;i++ ){
		sTemp=url.substr(url.length-s[i].length-1);
		sTemp=sTemp.toUpperCase();
		s[i]="."+s[i];
		if (s[i]==sTemp){
			b=true;
			break;
		}
	}
	return b;
}
</script>
</body>
</html>
<% 
End Sub 

' 保存操作
Sub DoSave()
	Dim oUpload, oFile, sFileExt, sFileName
	dim osize,username,rs
	' 建立上传对象
	Set oUpload = New upfile_class
	' 取得上传数据,限制最大上传

	
	
	'Call Checksize(osize)
	
	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 = UCase(oFile.FileExt)
	osize = oFile.Filesize
	Call CheckValidExt(sFileExt)


	Dim sRnd
	Randomize
	sRnd = Int(900 * Rnd) + 100
	sFileName = year(now) & month(now) & day(now) & hour(now) & minute(now) & second(now) & sRnd & "." & sFileExt
	response.write sfilename
	oFile.SaveToFile Server.Mappath(sUploadDir & "/"& sFileName)
	
	Set oFile = Nothing
	Set oUpload = Nothing
	
	if Request.Cookies(cookiesname)("UserName")<>"" then
		username=trim(Request.Cookies(cookiesname)("UserName"))
		set rs=conn.execute("select upfiles from [user] where username='"&username&"'")
		if not rs.eof then
			if rs(0)<>"" then
				conn.execute("update [user] set upfiles='"&rs(0)&"|"&sFileName&"' where username='"&username&"'")	
			else
				conn.execute("update [user] set upfiles='"&sFileName&"' where username='"&username&"'")	
			end if
			conn.execute("update [user] set upfiles_size=upfiles_size+"&osize&" where username='"&username&"'")
		end if
	end if
	set rs=nothing
	call closeconn()
	Call OutScript("parent.UploadSaved('" & sUploadDir & "/"& sFileName & "')")

End Sub

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

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

Sub Checksize(osize)
	If osize>nAllowSize*1024 Then
		call OutScript("parent.UploadError('你上传的文件总大小超出了最大限制（" & nAllowSize & "KB）！')")
		Response.End
	End If
End Sub


' 初始化上传限制数据
Sub InitUpload()
	
	dim rs
	dim user_maxsize,vip_maxsize,admin_maxsize,username
	dim theuped
		
	username=trim(Request.Cookies(cookiesname)("UserName"))
	set rs=conn.execute("select upfiles_size from [user] where username='"&username&"'")
	if not rs.eof then
		theuped=rs(0)
	end if

	set rs=conn.execute("select upfile_path,upfile_user,upfile_user_type,upfile_user_size,upfile_vip,upfile_vip_type,upfile_vip_size,upfile_admin,upfile_admin_type,upfile_admin_size,upfile_user_maxsize,upfile_vip_maxsize,upfile_admin_maxsize from bloginfo")
	user_maxsize=rs("upfile_user_maxsize")
	vip_maxsize=rs("upfile_vip_maxsize")
	admin_maxsize=rs("upfile_admin_maxsize")
	sUploadDir = rs("upfile_path")

		Select Case Request.Cookies(cookiesname)("userlevel")
		Case 7
			if rs("upfile_user")="true" then
				themax=round(user_maxsize-theuped/1024)
				sAllowExt = rs("upfile_user_type")
				if themax>rs("upfile_user_size") then
					nAllowSize = rs("upfile_user_size")
				else
					nAllowSize = themax
				end if
			else
				sAllowExt = "暂无上传权限"
				nAllowSize = 0
			end if
		Case 8
			if rs("upfile_vip")="true" then
				themax=round(vip_maxsize-theuped/1024)
				sAllowExt = rs("upfile_vip_type")
				if themax>rs("upfile_vip_size") then
					nAllowSize = rs("upfile_vip_size")
				else
					nAllowSize = themax
				end if
			else
				sAllowExt = "暂无上传权限"
				nAllowSize = 0
			end if
		Case 9
			if rs("upfile_admin")="true" then
				themax=round(admin_maxsize-theuped/1024)
				sAllowExt = rs("upfile_admin_type")
				if themax>rs("upfile_admin_size") then
					nAllowSize = rs("upfile_admin_size")
				else
					nAllowSize = themax
				end if
			else				
				sAllowExt = "暂无上传权限"
				nAllowSize = 0
			end if
		Case Else
			if session("AdminName")<>"" then
				sAllowExt = UCase("jpg|gif|png|bmp|swf|zip|rar")
				nAllowSize = 999999
			else
				sAllowExt = "错误！"
				nAllowSize = 0
			end if
		End Select
	sAllowExt = Replace(UCase(sAllowExt), "ASP", "")
	set rs=nothing	
End Sub
%>