我自己写的自定义Web的上传控件

时间:2012-06-14 08:39:23
【文件属性】:

文件名称:我自己写的自定义Web的上传控件

文件大小:5KB

文件格式:TXT

更新时间:2012-06-14 08:39:23

Web的上传控件

using system; using system.web.ui; using system.web.ui.webcontrols; using system.componentmodel; using system.web.ui.htmlcontrols; using system.io; using system.drawing; using system.drawing.design; namespace yingnet.common { ///

/// fileupload 的摘要说明。e:\program\common\fileupload.bmp /// [toolboxbitmap(typeof(yingnet.common.fileupload), "fileupload.bmp"), defaultproperty("text"), defaultevent("click"), toolboxdata("<{0}:fileupload runat=server>")] public class fileupload : system.web.ui.webcontrols.webcontrol { /// /// 上传按钮 /// private button button=new button(); /// /// 上传文件个数 /// private int filenum=1; /// /// file对象 /// private htmlinputfile[] file; /// /// 保存路径,默认为系统的临时目录 /// private string path=system.io.path.gettemppath(); /// /// 上传的文件名组 /// private string[] filename; /// /// 后缀文件名组 /// private string[] suffix; /// ///过滤器,写法是.txt;.abc /// private string filter=""; /// /// 限制文件上传大小,为0是不限制,单位是字节 /// private int size=0;//system.componentmodel.defaulteventattribute /// /// 上传事件 /// [bindable(true),category("事件"),description("上传后激发的事件") ] public event eventhandler click; /// /// 上传文件数 /// [bindable(true), category("参数"),description("设定上传文件的个数"), defaultvalue("1")] public int filenum{ set{ if(value<1){ value=1; } filenum=value; this.controls.clear(); file=new htmlinputfile[filenum]; filename=new string[filenum]; suffix=new string[filenum]; for(int i=0;i /// 上传按钮的文本 /// [bindable(true), category("参数"), description("设定上传文件的路径"), defaultvalue("1")] /// /// 上传路径 /// public string uploadpath { set{ if("".equals(value)||value==null){ value=system.io.path.gettemppath(); } path=value; } get{ return path; } } /// /// 得到文件名 /// public string[] filename{ get{ return filename; } } /// /// 得到后缀 /// public string[] suffix{ get{ return suffix; } } /// /// 过滤器 /// public string filter{ set{ filter=value; } get{ return filter; } } /// /// 限制大小 /// public int filesize{ set{ size=value; } get{ return size; } } /// /// 快捷键 /// public override string accesskey{ get{ return button.accesskey; } set{ button.accesskey=value; } } /// /// 背景 /// public override system.drawing.color backcolor{ get{ return button.backcolor; } set{ button.backcolor=value; } } /// /// 边框颜色 /// public override system.drawing.color bordercolor{ get{ return button.bordercolor; } set{ button.bordercolor=value; } } /// /// 边框风格 /// public override borderstyle borderstyle{ get{ return button.borderstyle; } set{ button.borderstyle=value; } } /// /// 文本 /// [bindable(true), category("appearance"), defaultvalue("")] public string text { get { return button.text; } set { button.text = value; } } public fileupload():base(){ filenum=1; button.click+=new eventhandler(this.button_click); this.controls.add(button); } /// /// 按钮事件 /// /// /// private void button_click(object sender, eventargs e){ upload(); ///添加你的代码 if(click!=null) click(sender,e); ///抛处事件 } /// /// 上传 /// private void upload(){ system.web.httppostedfile postedfile; for(int i=0;isize && size!=0){ break; } string suf=getsuffix(postedfile.filename); if(filter!=null && filter.indexof(suf)>-1){ break; } filename[i]=datetime.now.ticks.tostring(); suffix[i]=suf; postedfile.saveas(system.io.path.combine(path,filename[i]+suf)); } }finally{ filename[i]=""; } } } /// /// 获取后缀名 /// /// 文件名 /// 返回带.的后缀名 private string getsuffix(string filename){ int index=filename.lastindexof("."); if(index>0){ return filename.substring(index); } return ""; } } }


网友评论