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 file[i]=new HtmlInputFile(); this.Controls.Add(file[i]); } this.Controls.Add(button); } get{ return filenum; } } /// /// 上传按钮的文本 /// [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{ returnsuffix; } } /// /// 过滤器 /// 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;i try{ postedFile=file[i].PostedFile; if(postedFile!=null) { if(postedFile.ContentLength>size && 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 ""; }
} }
|