论Web控件开发 - 完美上传下载控件

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

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

其界面如下:
upload.jpg
测试站点为: http://www.keyss.cn:8888 (并不一直开放)

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

其主最要的代码如下:
None.gif          protected   override   void  Render(HtmlTextWriter writer)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif
InBlock.gif            writer.AddAttribute(HtmlTextWriterAttribute.Cellpadding, 
"0");
InBlock.gif            writer.AddAttribute(HtmlTextWriterAttribute.Cellspacing, 
"0");
InBlock.gif            
if(this.Width!=Unit.Empty)
InBlock.gif                writer.AddAttribute(HtmlTextWriterAttribute.Width, Width.ToString());
InBlock.gif            writer.AddAttribute(HtmlTextWriterAttribute.Border, 
"0");
InBlock.gif            
//table
InBlock.gif
            writer.RenderBeginTag(HtmlTextWriterTag.Table);
InBlock.gif            writer.RenderBeginTag(HtmlTextWriterTag.Tr);
InBlock.gif            
//cell1
InBlock.gif
            writer.AddAttribute(HtmlTextWriterAttribute.Width, "98%");
InBlock.gif            writer.RenderBeginTag(HtmlTextWriterTag.Td);
InBlock.gif            
this.FileUpload.Attributes.Add("id",this.ClientID+"__file");
InBlock.gif            
this.FileUpload.RenderControl(writer);
InBlock.gif            writer.RenderEndTag();
InBlock.gif            
//cell2
InBlock.gif
            writer.AddAttribute(HtmlTextWriterAttribute.Nowrap,"Yes");
InBlock.gif            writer.RenderBeginTag(HtmlTextWriterTag.Td);
InBlock.gif            
//hiden input file
InBlock.gif
            this.TxtFileName.Attributes.Add("id",this.ClientID);
InBlock.gif            
this.TxtFileName.RenderControl(writer);
InBlock.gif            
//upload btn
InBlock.gif
            WriteSpace(writer);
InBlock.gif            
string onClick = this.BtnUpLoad.Attributes["onclick"]==null?string.Empty:this.BtnUpLoad.Attributes["onclick"];
InBlock.gif            
this.BtnUpLoad.Attributes["onclick"= string.Format("if(!({0}_obj.CheckUpload())) return false;",this.ClientID) + onClick;
InBlock.gif            
this.BtnUpLoad.RenderControl(writer);
InBlock.gif
InBlock.gif            
//delete btn
InBlock.gif
            if(this.AllowDelete) WriteSpace(writer); 
InBlock.gif            onClick 
= (this.BtnDelete.Attributes["onclick"]==null)?string.Empty:this.BtnDelete.Attributes["onclick"];
InBlock.gif            
this.BtnDelete.Attributes["onclick"= string.Format("if(!({0}_obj.CheckDelete())) return false;",this.ClientID)+ onClick;
InBlock.gif            
this.BtnDelete.RenderControl(writer);
InBlock.gif
InBlock.gif
InBlock.gif            
//download btn
InBlock.gif
            WriteSpace(writer);
InBlock.gif            
this.BtnDownload.Attributes["onclick"]=string.Format("if(!({0}_obj.CheckDownload(this))) return false;",this.ClientID);
InBlock.gif            
this.BtnDownload.RenderControl(writer);
InBlock.gif            writer.RenderEndTag();
InBlock.gif            
InBlock.gif            writer.RenderEndTag();
InBlock.gif            writer.RenderEndTag();
ExpandedBlockEnd.gif        }

None.gif        
None.gif        
protected   override   void  CreateChildControls()
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
this.Controls.Clear();
InBlock.gif            
//button hidden ctl0
InBlock.gif
            this._txtFileName = new System.Web.UI.HtmlControls.HtmlInputHidden();
InBlock.gif            
this.Controls.Add(this._txtFileName);
InBlock.gif
InBlock.gif            
//file upload ctl1
InBlock.gif
            this._fileUpload = new System.Web.UI.HtmlControls.HtmlInputFile();
InBlock.gif            
this._fileUpload.Style.Add("WIDTH","100%");
InBlock.gif            
if(this.CSS!=string.Empty)
InBlock.gif                
this._fileUpload.Attributes.Add("class",this.CSS);
InBlock.gif            
this.Controls.Add(this._fileUpload);
InBlock.gif
InBlock.gif            
//button upload
InBlock.gif
            this._btnUpLoad = new ImageButton();
InBlock.gif            
this._btnUpLoad.ImageUrl = this.UploadImg;
InBlock.gif            
if(this.UploadFileNameType != FileNameType.AutoGenerate)
InBlock.gif                
this._btnUpLoad.EnableWarning = true;
InBlock.gif            
else
InBlock.gif                
this._btnUpLoad.EnableWarning = false;
InBlock.gif            
this._btnUpLoad.AlternateText = this.UploadAlt;
InBlock.gif            
this._btnUpLoad.WarningMessage = "如果文件已经存在将被覆盖,确定吗?";
InBlock.gif            
this._btnUpLoad.CausesValidation = false;
InBlock.gif            
this._btnUpLoad.Click += new ImageClickEventHandler(Upload_Clicked);
InBlock.gif            
this.Controls.Add(this._btnUpLoad);
InBlock.gif
InBlock.gif            
//button delete
InBlock.gif
            this._btnDelete = new ImageButton();
InBlock.gif            
this._btnDelete.ImageUrl = this.DeleteImg;
InBlock.gif            
this._btnDelete.EnableWarning = true;
InBlock.gif            
this._btnDelete.AlternateText = this.DeleteAlt;
InBlock.gif            
this._btnDelete.WarningMessage = "确定要删除吗?";
InBlock.gif            
this._btnDelete.CausesValidation = false;
InBlock.gif            
this._btnDelete.Click +=new ImageClickEventHandler(Delete_Clicked);
InBlock.gif            
if(!this.AllowDelete)
InBlock.gif                
this._btnDelete.Visible = false;
InBlock.gif            
this.Controls.Add(this._btnDelete);
InBlock.gif
InBlock.gif            
//button download
InBlock.gif
            this._btnDownload = new HyperLink();
InBlock.gif            
this._btnDownload.Style.Add("cusor","hand");
InBlock.gif            
this._btnDownload.ImageUrl = this.DownloadImg;
InBlock.gif            
this._btnDownload.ToolTip = this.DownloadAlt;
InBlock.gif            
this._btnDownload.NavigateUrl = this.FullFileName;
InBlock.gif            
this._btnDownload.Target = "_blank";
InBlock.gif            
this._btnDownload.Text = "download";
InBlock.gif
InBlock.gif            
this.Controls.Add(this._btnDownload);
InBlock.gif
InBlock.gif            ChildControlsCreated 
= true;
ExpandedBlockEnd.gif        }

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

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

由于OA系统是基于WEB方式的,如果只判断用户是否点击“下载文件”,就来判断用户是否成功下载了文件。这种方式很显然行不通,怎么办呢?我想到的就是用一控件,然后用回调事件来处理数据库方面的问题(下载记录问题) 有兴趣的朋友可在 http://www.interdrp.com/ 下载分销系统 用测试帐号进系统后,点测试程序,再点WEB下载 可以看见效果 https://www.interdrp.com/software/ReYoWebDownLoad.zip(点击下载控件) API说明 ReYoWebDL.copyright="锐洋软件拥有版权 http://www.interdrp.com/" //必须 ReYoWebDL.url="http://dl.baofeng.com/storm3/Storm2009-0504-1.exe" //下载文件的路径 ReYoWebDL.path ="" //保存文件地址 ReYoWebDL.ReYoStartDownload() //下载动作 ReYoWebDL.ReYoStopDownload() //停止下载 ReYoWebDL.size //下载文件大小 ReYoWebDL.bytes//已下载大小 ReYoWebDL.speed //下载速度 KB/S ReYoWebDL.done //下载是否完成 ReYoWebDL.cancle=true; //是否取消下载 ReYoWebDL.urlsource //下载文件名 ReYoWebDL.percent //下载百分比 升级提示: 20090506:新增剩余时间,下载百分比进度,下载速度及文件保存位置显示;且支持断点续传功能。 20090504:新增下载过程序取消下载动作。 20090502:新增文件存在提示。 目前控件支持两种方式下载: 1:开发者在程序中指定下载文件的url,保存文件的path。例如:download("http://www.interdrp.com/software/hotel/setup.zip","c:\a.zip" 2:开发者在程序中指定下载文件的url,不指定保存文件的path,系统会提示您选择保存文件的路径。例如:download("http://www.interdrp.com/software/hotel/setup.zip",""
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值