论Web控件开发 - 完美上传下载控件“新”(四)

对于客户端事件比较复杂,首先介绍一下客户端的脚本:
首先我在客户端声名一个KSS_Upload类的定义,它主要帮助我来实现客户端的验证
ExpandedBlockStart.gif ContractedBlock.gif /**/ /************************************************************************/
ExpandedBlockStart.gifContractedBlock.gif
/**/ /* Keyss.WebControls.Upload -- Version 1.0.0                            */
ExpandedBlockStart.gifContractedBlock.gif
/**/ /* Client Script for Internet Explorer 5.0 or Above                        */
ExpandedBlockStart.gifContractedBlock.gif
/**/ /*----------------------------------------------------------------------*/
ExpandedBlockStart.gifContractedBlock.gif
/**/ /* Date:        July 25, 2004                                            */
ExpandedBlockStart.gifContractedBlock.gif
/**/ /* Copyright:    2001-2004, www.keyss.cn. All Rights Reserved            */
ExpandedBlockStart.gifContractedBlock.gif
/**/ /* Email:        keyss@21cn.com                                            */
ExpandedBlockStart.gifContractedBlock.gif
/**/ /************************************************************************/
None.giffunction KSS_Upload(id, extFilter, path)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
//对象ID
InBlock.gif
    this.ID = id;
InBlock.gif    
//上传文件路径
InBlock.gif
    this.Path = (!path)?'':path;
InBlock.gif    
//扩展名验证对象
InBlock.gif
    this.ExtFilter = extFilter;
InBlock.gif    
//当点击上传按钮时客户端验证函数
InBlock.gif
    this.CheckUpload = function()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
//this.ID + "__file"为htmlfileupload控件id
InBlock.gif
        var fileName = document.getElementById(this.ID + "__file").value;
InBlock.gif        
//验证fileupload中的文件名是否合法
InBlock.gif
        if (!((fileName)&&(fileName!='')&&(fileName.match(/^([A-Za-z]:\\(.)+\\[^\\]+)$/))))
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            alert(
"文件名称格式有误!");
InBlock.gif            
return false;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
//是否对扩展名要求验证
InBlock.gif
        if(!this.ExtFilter) return true;
InBlock.gif        
if(!fileName.match(this.ExtFilter))
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            alert(
"文件种类不支持!");
InBlock.gif            
return false;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
return true;
ExpandedSubBlockEnd.gif    }

InBlock.gif    
//当点击删除按钮时客户端验证函数
InBlock.gif
    this.CheckDelete = function()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
//this.ID同时为inputhidden控件的id
InBlock.gif
        var txtFileName = document.getElementById(this.ID);
InBlock.gif        var fileName 
= txtFileName.value;
InBlock.gif        
//是否有删除的文件
InBlock.gif
        if (!(fileName&&(fileName!='')))
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            alert(
"文件名称格式有误!");
InBlock.gif            
return false;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
return true;
ExpandedSubBlockEnd.gif    }

InBlock.gif    
//当下载时客户端验证函数
InBlock.gif
    this.CheckDownload = function(obj)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{    
InBlock.gif        
//this.ID同时为inputhidden控件的id
InBlock.gif

InBlock.gif        var txtFileName 
= document.getElementById(this.ID);
InBlock.gif        var fileName 
= txtFileName.value;
InBlock.gif        var fullFileName 
= this.Path + fileName;
InBlock.gif        
//是否有可下载的文件
InBlock.gif
        if (!(fileName&&(fileName!='')))
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            alert(
"文件名称格式有误!");
InBlock.gif            
return false;
ExpandedSubBlockEnd.gif        }

InBlock.gif        obj.href 
= fullFileName;
InBlock.gif        
return true;
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

而当控件在生成期间通过以下代码实现,客户端upload对象的声明,其中参数{0}为控件的ID,{1}为扩展名验证器,{2}为上传文件的路径.

None.gif
None.gif
< SCRIPT language = " JavaScript " >
None.gif    
var  { 0 }_obj  =   new  KSS_Upload( " {0} " , { 1 },  " {2} " );
None.gif
</ SCRIPT >
None.gif

以下是服务器端客户事件相关代码
None.gif   // 获得后缀名验证器字符串
None.gif
   protected   virtual   string  GetExtValidateStr()
ExpandedBlockStart.gifContractedBlock.gif  
dot.gif {
InBlock.gif   
if(this.ExtFilters.Count > 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    System.Text.StringBuilder st 
= new System.Text.StringBuilder();
InBlock.gif    st.Append(
"/(");
InBlock.gif    
for(int i = 0; i<ExtFilters.Count;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif     
if(i != 0)
InBlock.gif      st.Append(
"|");
InBlock.gif     st.AppendFormat(
"(\\{0})",this.ExtFilters[i].Text.ToLower());
ExpandedSubBlockEnd.gif    }

InBlock.gif    st.Append(
")$/");
InBlock.gif    
return st.ToString();
ExpandedSubBlockEnd.gif   }

InBlock.gif   
else
InBlock.gif    
return "null";
InBlock.gif  
ExpandedBlockEnd.gif  }

ContractedBlock.gifExpandedBlockStart.gif  
static #region static
InBlock.gif
//获取前面的jscript角本字符串
InBlock.gif
  protected static string _webUploadScript;
InBlock.gif  
protected static string _declareWebUploadScript;
InBlock.gif  
static Upload()
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   System.Reflection.Assembly assembly 
= System.Reflection.Assembly.GetExecutingAssembly();
InBlock.gif   
if(assembly != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    System.IO.StreamReader reader 
= new System.IO.StreamReader(assembly.GetManifestResourceStream("Keyss.WebControls.WebUpload.upload.js"));
InBlock.gif    Upload._webUploadScript 
= reader.ReadToEnd();
InBlock.gif    reader.Close();
InBlock.gif    reader 
= new System.IO.StreamReader(assembly.GetManifestResourceStream("Keyss.WebControls.WebUpload.startup.js"));
InBlock.gif    Upload._declareWebUploadScript 
= reader.ReadToEnd();
InBlock.gif    reader.Close();
ExpandedSubBlockEnd.gif   }
 
ExpandedSubBlockEnd.gif  }

ExpandedBlockEnd.gif  
#endregion

None.gif  
protected   override   void  OnPreRender(EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif  
dot.gif
InBlock.gif
//注册KSS_Upload类
InBlock.gif
   if(!Page.IsClientScriptBlockRegistered("Keyss.WebControls.Upload"))
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    Page.RegisterClientScriptBlock(
"Keyss.WebControls.Upload",Upload._webUploadScript);
ExpandedSubBlockEnd.gif   }

InBlock.gif
//声明客户端jscript upload_obj对象
InBlock.gif
   if(!Page.IsStartupScriptRegistered(this.ClientID))
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    
string tmpStr = this.GetExtValidateStr();
InBlock.gif    
InBlock.gif    Page.RegisterStartupScript(ClientID,
string.Format(Upload._declareWebUploadScript,ClientID,tmpStr,this.FilePath));
ExpandedSubBlockEnd.gif   }

ExpandedBlockEnd.gif  }

None.gif
 

转载于:https://www.cnblogs.com/keyss/archive/2005/01/22/95823.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值