1
using System;
2
using System.Drawing;
3
using System.Drawing.Design;
4
using System.Web.UI;
5
using System.Web;
6
using System.Text;
7
using System.Web.UI.WebControls;
8
using System.Web.UI.HtmlControls;
9
using System.ComponentModel;
10
using System.ComponentModel.Design;
11
12
namespace 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("" + 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-" + 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

2

3

4

5

6

7

8

9

10

11

12

13



14

15



16


17

18

19

20

21

22

23

24



25

26

27


28

29

30

31



32



33



34

35

36

37

38

39

40



41

42



43

44

45

46

47

48



49

50

51

52

53

54

55



56

57



58

59

60

61

62

63

64

65

66

67



68



69



70

71

72

73

74

75

76



77

78



79

80

81



82

83

84

85

86

87

88

89


90

91



92

93



94

95



96

97



98

99

100



101

102

103

104

105

106

107

108

109

110

111



112

113

114

115

116

117



118

119

120

121

122

123

124

125

126

127


128

129

130



131

132

133

134

135



136

137

138

139



140

141

142

143



144

145

146

147



148

149

150

151



152

153

154

155



156

157

158

159



160

161

162

163



164

165

166

167



168

169

170

171



172

173

174

175



176

177

178

179



180

181

182

183



184

185

186

187



188

189

190

191

192

193

194
