对于客户端事件比较复杂,首先介绍一下客户端的脚本:
首先我在客户端声名一个KSS_Upload类的定义,它主要帮助我来实现客户端的验证
/**/
/************************************************************************/
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**/
/* Keyss.WebControls.Upload -- Version 1.0.0 */
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**/
/* Client Script for Internet Explorer 5.0 or Above */
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**/
/*----------------------------------------------------------------------*/
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**/
/* Date: July 25, 2004 */
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**/
/* Copyright: 2001-2004, www.keyss.cn. All Rights Reserved */
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**/
/* Email: keyss@21cn.com */
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**/
/************************************************************************/
function KSS_Upload(id, extFilter, path)
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
//对象ID
this.ID = id;
//上传文件路径
this.Path = (!path)?'':path;
//扩展名验证对象
this.ExtFilter = extFilter;
//当点击上传按钮时客户端验证函数
this.CheckUpload = function()
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//this.ID + "__file"为htmlfileupload控件id
var fileName = document.getElementById(this.ID + "__file").value;
//验证fileupload中的文件名是否合法
if (!((fileName)&&(fileName!='')&&(fileName.match(/^([A-Za-z]:\\(.)+\\[^\\]+)$/))))
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
alert("文件名称格式有误!");
return false;
}
//是否对扩展名要求验证
if(!this.ExtFilter) return true;
if(!fileName.match(this.ExtFilter))
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
alert("文件种类不支持!");
return false;
}
return true;
}
//当点击删除按钮时客户端验证函数
this.CheckDelete = function()
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//this.ID同时为inputhidden控件的id
var txtFileName = document.getElementById(this.ID);
var fileName = txtFileName.value;
//是否有删除的文件
if (!(fileName&&(fileName!='')))
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
alert("文件名称格式有误!");
return false;
}
return true;
}
//当下载时客户端验证函数
this.CheckDownload = function(obj)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//this.ID同时为inputhidden控件的id
var txtFileName = document.getElementById(this.ID);
var fileName = txtFileName.value;
var fullFileName = this.Path + fileName;
//是否有可下载的文件
if (!(fileName&&(fileName!='')))
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
alert("文件名称格式有误!");
return false;
}
obj.href = fullFileName;
return true;
}
}
而当控件在生成期间通过以下代码实现,客户端upload对象的声明,其中参数{0}为控件的ID,{1}为扩展名验证器,{2}为上传文件的路径.
<
SCRIPT language
=
"
JavaScript
"
>
var
{
0
}_obj
=
new
KSS_Upload(
"
{0}
"
, {
1
},
"
{2}
"
);
</
SCRIPT
>
以下是服务器端客户事件相关代码
//
获得后缀名验证器字符串
protected
virtual
string
GetExtValidateStr()
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
if(this.ExtFilters.Count > 0)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
System.Text.StringBuilder st = new System.Text.StringBuilder();
st.Append("/(");
for(int i = 0; i<ExtFilters.Count;i++)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(i != 0)
st.Append("|");
st.AppendFormat("(\\{0})",this.ExtFilters[i].Text.ToLower());
}
st.Append(")$/");
return st.ToString();
}
else
return "null";
}
![ContractedBlock.gif](/Images/OutliningIndicators/ContractedBlock.gif)
static
#region static
//获取前面的jscript角本字符串
protected static string _webUploadScript;
protected static string _declareWebUploadScript;
static Upload()
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();
if(assembly != null)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
System.IO.StreamReader reader = new System.IO.StreamReader(assembly.GetManifestResourceStream("Keyss.WebControls.WebUpload.upload.js"));
Upload._webUploadScript = reader.ReadToEnd();
reader.Close();
reader = new System.IO.StreamReader(assembly.GetManifestResourceStream("Keyss.WebControls.WebUpload.startup.js"));
Upload._declareWebUploadScript = reader.ReadToEnd();
reader.Close();
}
}
#endregion
protected
override
void
OnPreRender(EventArgs e)
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
//注册KSS_Upload类
if(!Page.IsClientScriptBlockRegistered("Keyss.WebControls.Upload"))
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Page.RegisterClientScriptBlock("Keyss.WebControls.Upload",Upload._webUploadScript);
}
//声明客户端jscript upload_obj对象
if(!Page.IsStartupScriptRegistered(this.ClientID))
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
string tmpStr = this.GetExtValidateStr();
Page.RegisterStartupScript(ClientID,string.Format(Upload._declareWebUploadScript,ClientID,tmpStr,this.FilePath));
}
}