一个自定义的文件上传控件
Asp.net中的HtmlInputFile是继承自HtmlInput的一种客户端控件,下面,我们自定义一个WebControl以实现HtmlInputFile一样的功能的控件。
1using System;
2using System.Drawing;
3using System.Drawing.Design;
4using System.Web.UI;
5using System.Web;
6using System.Text;
7using System.Web.UI.WebControls;
8using System.Web.UI.HtmlControls;
9using System.ComponentModel;
10using System.ComponentModel.Design;
11
12namespace FaibClass.WebControls
13{
14 public abstract class BaseFileUploader : WebControl
15 {
16 私有变量#region 私有变量
17 private bool m_Disabled = false;
18 private string m_Accept = "";
19
20 protected HtmlForm Form;
21 #endregion
22
23 public BaseFileUploader()
24 {
25 }
26
27 属性#region 属性
28 [Category("Appearance")]
29 [DefaultValue(false)]
30 public bool Disabled
31 {
32 get{return m_Disabled;}
33 set{m_Disabled = value;}
34 }
35
36 [Category("Appearance")]
37 [DefaultValue("")]
38 [Description("文件名。")]
39 public string FileName
40 {
41 get
42 {
43 if(ViewState["FileName"] == null)
44 return "";
45 else
46 return ViewState["FileName"].ToString();
47 }
48 set{ViewState["FileName"] = value;}
49 }
50
51 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
52 [Browsable(false)]
53 [Category("Default")]
54 public HttpPostedFile PostedFile
55 {
56 get
57 {
58 return this.Context.Request.Files[this.UniqueID];
59 }
60 }
61
62 [Category("Behavior")]
63 [Description("获取或设置用逗号分隔的 MIME 编码列表,该列表用于约束用户可以选择的文件类型。")]
64 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
65 [DefaultValue("")]
66 public string Accept
67 {
68 get{return m_Accept;}
69 set{m_Accept = value;}
70 }
71
72 [Browsable(false)]
73 [Description(" 获取客户机上文件的完整路径。")]
74 [Category("Default")]
75 public string Value
76 {
77 get
78 {
79 HttpPostedFile postedFile = this.PostedFile;
80 if (postedFile != null)
81 {
82 return postedFile.FileName;
83 }
84 return string.Empty;
85 }
86 }
87 #endregion
88
89 公共方法#region 公共方法
90 protected override void OnPreRender(EventArgs e)
91 {
92 if(!Page.IsClientScriptBlockRegistered("FormEnctype"))
93 {
94 foreach(Control ctl in this.Page.Controls)
95 {
96 if(ctl is HtmlForm)
97 {
98 Form = (HtmlForm)ctl;
99 if (Form.Enctype.Length == 0)
100 {
101 Form.Enctype = "multipart/form-data";
102 }
103 Page.RegisterClientScriptBlock("FormEnctype", "");
104 break;
105 }
106 }
107 }
108 }
109
110 protected override void Render(HtmlTextWriter writer)
111 {
112 WriteStyle(writer);
113 writer.AddAttribute("type", "file");
114 writer.AddAttribute("id", this.ClientID);
115 writer.AddAttribute("name", this.UniqueID);
116 if(m_Disabled)
117 {
118 writer.AddAttribute("disabled", "true");
119 }
120 writer.AddAttribute("accept", m_Accept);
121
122 writer.RenderBeginTag(HtmlTextWriterTag.Input);
123 writer.RenderEndTag();
124 }
125 #endregion
126
127 私有方法#region 私有方法
128 //输出样式
129 private void WriteStyle(HtmlTextWriter output)
130 {
131 StringBuilder strStyle = new StringBuilder();
132 output.AddAttribute(HtmlTextWriterAttribute.Id, this.ClientID);
133
134 if(this.Width != Unit.Empty)
135 {
136 strStyle.Append("width:" + this.Width.ToString() + ";");
137 }
138 if(this.Height != Unit.Empty)
139 {
140 strStyle.Append("height:" + this.Height.ToString() + ";");
141 }
142 if(strStyle.Length != 0)
143 {
144 output.AddAttribute(HtmlTextWriterAttribute.Style, strStyle.ToString());
145 }
146 if(this.ToolTip != null && this.ToolTip != "")
147 {
148 output.AddAttribute(HtmlTextWriterAttribute.Title,this.ToolTip);
149 }
150 if(this.ForeColor != Color.Empty)
151 {
152 strStyle.Append("color:" + Util.ConvertToHexColor(this.ForeColor) + ";");
153 }
154 if(this.BackColor != Color.Empty)
155 {
156 strStyle.Append("background-color:" + Util.ConvertToHexColor(this.BackColor) + ";");
157 }
158 if(this.BorderStyle != BorderStyle.NotSet)
159 {
160 strStyle.Append("border-style:" + this.BorderStyle.ToString() + ";");
161 }
162 if(this.BorderWidth != Unit.Empty)
163 {
164 strStyle.Append("border-width:" + this.BorderWidth.ToString() + ";");
165 }
166 if(this.BorderColor != Color.Empty)
167 {
168 strStyle.Append("border-color:" + Util.ConvertToHexColor(this.BorderColor) + ";");
169 }
170 if(this.Font.Name != null && this.Font.Name != "")
171 {
172 strStyle.Append("font-family:" + this.Font.Name + ";");
173 }
174 if(this.Font.Size != FontUnit.Empty)
175 {
176 strStyle.Append("font-size:" + this.Font.Size + ";");
177 }
178 if(this.Font.Bold)
179 {
180 strStyle.Append("font-weight:bold;");
181 }
182 if(strStyle.Length != 0)
183 {
184 output.AddAttribute(HtmlTextWriterAttribute.Style, strStyle.ToString());
185 }
186 if(CssClass != null & CssClass != "")
187 {
188 output.AddAttribute(HtmlTextWriterAttribute.Class, CssClass);
189 }
190 }
191 #endregion
192 }
193}
194
2using System.Drawing;
3using System.Drawing.Design;
4using System.Web.UI;
5using System.Web;
6using System.Text;
7using System.Web.UI.WebControls;
8using System.Web.UI.HtmlControls;
9using System.ComponentModel;
10using System.ComponentModel.Design;
11
12namespace FaibClass.WebControls
13{
14 public abstract class BaseFileUploader : WebControl
15 {
16 私有变量#region 私有变量
17 private bool m_Disabled = false;
18 private string m_Accept = "";
19
20 protected HtmlForm Form;
21 #endregion
22
23 public BaseFileUploader()
24 {
25 }
26
27 属性#region 属性
28 [Category("Appearance")]
29 [DefaultValue(false)]
30 public bool Disabled
31 {
32 get{return m_Disabled;}
33 set{m_Disabled = value;}
34 }
35
36 [Category("Appearance")]
37 [DefaultValue("")]
38 [Description("文件名。")]
39 public string FileName
40 {
41 get
42 {
43 if(ViewState["FileName"] == null)
44 return "";
45 else
46 return ViewState["FileName"].ToString();
47 }
48 set{ViewState["FileName"] = value;}
49 }
50
51 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
52 [Browsable(false)]
53 [Category("Default")]
54 public HttpPostedFile PostedFile
55 {
56 get
57 {
58 return this.Context.Request.Files[this.UniqueID];
59 }
60 }
61
62 [Category("Behavior")]
63 [Description("获取或设置用逗号分隔的 MIME 编码列表,该列表用于约束用户可以选择的文件类型。")]
64 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
65 [DefaultValue("")]
66 public string Accept
67 {
68 get{return m_Accept;}
69 set{m_Accept = value;}
70 }
71
72 [Browsable(false)]
73 [Description(" 获取客户机上文件的完整路径。")]
74 [Category("Default")]
75 public string Value
76 {
77 get
78 {
79 HttpPostedFile postedFile = this.PostedFile;
80 if (postedFile != null)
81 {
82 return postedFile.FileName;
83 }
84 return string.Empty;
85 }
86 }
87 #endregion
88
89 公共方法#region 公共方法
90 protected override void OnPreRender(EventArgs e)
91 {
92 if(!Page.IsClientScriptBlockRegistered("FormEnctype"))
93 {
94 foreach(Control ctl in this.Page.Controls)
95 {
96 if(ctl is HtmlForm)
97 {
98 Form = (HtmlForm)ctl;
99 if (Form.Enctype.Length == 0)
100 {
101 Form.Enctype = "multipart/form-data";
102 }
103 Page.RegisterClientScriptBlock("FormEnctype", "");
104 break;
105 }
106 }
107 }
108 }
109
110 protected override void Render(HtmlTextWriter writer)
111 {
112 WriteStyle(writer);
113 writer.AddAttribute("type", "file");
114 writer.AddAttribute("id", this.ClientID);
115 writer.AddAttribute("name", this.UniqueID);
116 if(m_Disabled)
117 {
118 writer.AddAttribute("disabled", "true");
119 }
120 writer.AddAttribute("accept", m_Accept);
121
122 writer.RenderBeginTag(HtmlTextWriterTag.Input);
123 writer.RenderEndTag();
124 }
125 #endregion
126
127 私有方法#region 私有方法
128 //输出样式
129 private void WriteStyle(HtmlTextWriter output)
130 {
131 StringBuilder strStyle = new StringBuilder();
132 output.AddAttribute(HtmlTextWriterAttribute.Id, this.ClientID);
133
134 if(this.Width != Unit.Empty)
135 {
136 strStyle.Append("width:" + this.Width.ToString() + ";");
137 }
138 if(this.Height != Unit.Empty)
139 {
140 strStyle.Append("height:" + this.Height.ToString() + ";");
141 }
142 if(strStyle.Length != 0)
143 {
144 output.AddAttribute(HtmlTextWriterAttribute.Style, strStyle.ToString());
145 }
146 if(this.ToolTip != null && this.ToolTip != "")
147 {
148 output.AddAttribute(HtmlTextWriterAttribute.Title,this.ToolTip);
149 }
150 if(this.ForeColor != Color.Empty)
151 {
152 strStyle.Append("color:" + Util.ConvertToHexColor(this.ForeColor) + ";");
153 }
154 if(this.BackColor != Color.Empty)
155 {
156 strStyle.Append("background-color:" + Util.ConvertToHexColor(this.BackColor) + ";");
157 }
158 if(this.BorderStyle != BorderStyle.NotSet)
159 {
160 strStyle.Append("border-style:" + this.BorderStyle.ToString() + ";");
161 }
162 if(this.BorderWidth != Unit.Empty)
163 {
164 strStyle.Append("border-width:" + this.BorderWidth.ToString() + ";");
165 }
166 if(this.BorderColor != Color.Empty)
167 {
168 strStyle.Append("border-color:" + Util.ConvertToHexColor(this.BorderColor) + ";");
169 }
170 if(this.Font.Name != null && this.Font.Name != "")
171 {
172 strStyle.Append("font-family:" + this.Font.Name + ";");
173 }
174 if(this.Font.Size != FontUnit.Empty)
175 {
176 strStyle.Append("font-size:" + this.Font.Size + ";");
177 }
178 if(this.Font.Bold)
179 {
180 strStyle.Append("font-weight:bold;");
181 }
182 if(strStyle.Length != 0)
183 {
184 output.AddAttribute(HtmlTextWriterAttribute.Style, strStyle.ToString());
185 }
186 if(CssClass != null & CssClass != "")
187 {
188 output.AddAttribute(HtmlTextWriterAttribute.Class, CssClass);
189 }
190 }
191 #endregion
192 }
193}
194