最近开发了一个文件上传下载控件,其支持的功能如下:

一、支持可设置不同权限级别的文件上传、下载、删除功能
二、支持验证器控件
三、支持扩展类型客户端和服务器端验证
四、支持FileSize属性

其界面如下:

测试站点为:http://www.keyss.cn:8888 (并不一直开放)

这个自定义控件主要由五个子控件组成一个htmlfileinput两个imagebutton(上传和删除)和一个Hyperlink(用来下载)另外一个为inputhidden用来保存文件名和支持客户端验证。

其主最要的代码如下:
        protected override void Render(HtmlTextWriter writer)
        
{

            writer.AddAttribute(HtmlTextWriterAttribute.Cellpadding, 
"0");
            writer.AddAttribute(HtmlTextWriterAttribute.Cellspacing, 
"0");
            
if(this.Width!=Unit.Empty)
                writer.AddAttribute(HtmlTextWriterAttribute.Width, Width.ToString());
            writer.AddAttribute(HtmlTextWriterAttribute.Border, 
"0");
            
//table
            writer.RenderBeginTag(HtmlTextWriterTag.Table);
            writer.RenderBeginTag(HtmlTextWriterTag.Tr);
            
//cell1
            writer.AddAttribute(HtmlTextWriterAttribute.Width, "98%");
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            
this.FileUpload.Attributes.Add("id",this.ClientID+"__file");
            
this.FileUpload.RenderControl(writer);
            writer.RenderEndTag();
            
//cell2
            writer.AddAttribute(HtmlTextWriterAttribute.Nowrap,"Yes");
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            
//hiden input file
            this.TxtFileName.Attributes.Add("id",this.ClientID);
            
this.TxtFileName.RenderControl(writer);
            
//upload btn
            WriteSpace(writer);
            
string onClick = this.BtnUpLoad.Attributes["onclick"]==null?string.Empty:this.BtnUpLoad.Attributes["onclick"];
            
this.BtnUpLoad.Attributes["onclick"= string.Format("if(!({0}_obj.CheckUpload())) return false;",this.ClientID) + onClick;
            
this.BtnUpLoad.RenderControl(writer);

            
//delete btn
            if(this.AllowDelete) WriteSpace(writer); 
            onClick 
= (this.BtnDelete.Attributes["onclick"]==null)?string.Empty:this.BtnDelete.Attributes["onclick"];
            
this.BtnDelete.Attributes["onclick"= string.Format("if(!({0}_obj.CheckDelete())) return false;",this.ClientID)+ onClick;
            
this.BtnDelete.RenderControl(writer);


            
//download btn
            WriteSpace(writer);
            
this.BtnDownload.Attributes["onclick"]=string.Format("if(!({0}_obj.CheckDownload(this))) return false;",this.ClientID);
            
this.BtnDownload.RenderControl(writer);
            writer.RenderEndTag();
            
            writer.RenderEndTag();
            writer.RenderEndTag();
        }

        
        
protected override void CreateChildControls()
        
{
            
this.Controls.Clear();
            
//button hidden ctl0
            this._txtFileName = new System.Web.UI.HtmlControls.HtmlInputHidden();
            
this.Controls.Add(this._txtFileName);

            
//file upload ctl1
            this._fileUpload = new System.Web.UI.HtmlControls.HtmlInputFile();
            
this._fileUpload.Style.Add("WIDTH","100%");
            
if(this.CSS!=string.Empty)
                
this._fileUpload.Attributes.Add("class",this.CSS);
            
this.Controls.Add(this._fileUpload);

            
//button upload
            this._btnUpLoad = new ImageButton();
            
this._btnUpLoad.ImageUrl = this.UploadImg;
            
if(this.UploadFileNameType != FileNameType.AutoGenerate)
                
this._btnUpLoad.EnableWarning = true;
            
else
                
this._btnUpLoad.EnableWarning = false;
            
this._btnUpLoad.AlternateText = this.UploadAlt;
            
this._btnUpLoad.WarningMessage = "如果文件已经存在将被覆盖,确定吗?";
            
this._btnUpLoad.CausesValidation = false;
            
this._btnUpLoad.Click += new ImageClickEventHandler(Upload_Clicked);
            
this.Controls.Add(this._btnUpLoad);

            
//button delete
            this._btnDelete = new ImageButton();
            
this._btnDelete.ImageUrl = this.DeleteImg;
            
this._btnDelete.EnableWarning = true;
            
this._btnDelete.AlternateText = this.DeleteAlt;
            
this._btnDelete.WarningMessage = "确定要删除吗?";
            
this._btnDelete.CausesValidation = false;
            
this._btnDelete.Click +=new ImageClickEventHandler(Delete_Clicked);
            
if(!this.AllowDelete)
                
this._btnDelete.Visible = false;
            
this.Controls.Add(this._btnDelete);

            
//button download
            this._btnDownload = new HyperLink();
            
this._btnDownload.Style.Add("cusor","hand");
            
this._btnDownload.ImageUrl = this.DownloadImg;
            
this._btnDownload.ToolTip = this.DownloadAlt;
            
this._btnDownload.NavigateUrl = this.FullFileName;
            
this._btnDownload.Target = "_blank";
            
this._btnDownload.Text = "download";

            
this.Controls.Add(this._btnDownload);

            ChildControlsCreated 
= true;
        }

重载了createchildcontrols 和render并且当上传或删除成功时触发afteroperation事件。在输出inputhidden时将clientID作为其id属性输出支持客户端验证。