2010-03-12 Jb Evain <jbevain@novell.com>
[mono.git] / mcs / class / System.Web / System.Web.UI.HtmlControls / HtmlInputFile.cs
1 //
2 // System.Web.UI.HtmlControls.HtmlInputFile.cs
3 //
4 // Author:
5 //      Dick Porter  <dick@ximian.com>
6 //
7 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 using System.Collections.Specialized;
30 using System.ComponentModel;
31 using System.Security.Permissions;
32
33 namespace System.Web.UI.HtmlControls 
34 {
35         // CAS
36         [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
37         [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
38         // attributes
39         [ValidationProperty ("Value")]
40         public class HtmlInputFile : HtmlInputControl , IPostBackDataHandler
41         {
42                 public HtmlInputFile () : base ("file")
43                 {
44                 }
45
46                 [DefaultValue ("")]
47                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
48                 [WebSysDescription("")]
49                 public string Accept 
50                 {
51                         get {
52                                 string acc = Attributes["accept"];
53
54                                 if (acc == null) {
55                                         return (String.Empty);
56                                 }
57
58                                 return (acc);
59                         }
60                         set {
61                                 if (value == null) {
62                                         Attributes.Remove ("accept");
63                                 } else {
64                                         Attributes["accept"] = value;
65                                 }
66                         }
67                 }
68
69                 [DefaultValue ("")]
70                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
71                 [WebSysDescription("")]
72                 public int MaxLength 
73                 {
74                         get {
75                                 string maxlen = Attributes["maxlength"];
76                                 
77                                 if (maxlen == null) {
78                                         return (-1);
79                                 } else {
80                                         return (Convert.ToInt32 (maxlen));
81                                 }
82                         }
83                         set {
84                                 if (value == -1) {
85                                         Attributes.Remove ("maxlength");
86                                 } else {
87                                         Attributes["maxlength"] = value.ToString ();
88                                 }
89                         }
90                 }
91
92                 HttpPostedFile posted_file;
93                 
94                 [DefaultValue ("")]
95                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
96                 [WebSysDescription("")]
97                 [WebCategory("Misc")]
98                 public HttpPostedFile PostedFile 
99                 {
100                         get {
101                                 return (posted_file);
102                         }
103                 }
104                 
105 #if NET_2_0
106                 [DefaultValue ("-1")]
107 #else
108                 [DefaultValue ("")]
109 #endif
110                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
111                 [WebSysDescription("")]
112                 [WebCategory("Appearance")]
113                 public int Size 
114                 {
115                         get {
116                                 string size = Attributes["size"];
117                                 
118                                 if (size == null) {
119                                         return (-1);
120                                 } else {
121                                         return (Convert.ToInt32 (size));
122                                 }
123                         }
124                         set {
125                                 if (value == -1) {
126                                         Attributes.Remove ("size");
127                                 } else {
128                                         Attributes["size"] = value.ToString ();
129                                 }
130                         }
131                 }
132                 
133                 [Browsable (false)]
134                 public override string Value 
135                 {
136                         get {
137                                 HttpPostedFile file = PostedFile;
138                                 if (file == null)
139                                         return string.Empty;
140
141                                 return file.FileName;
142                         }
143                         set {
144                                 throw new NotSupportedException ("The value property on HtmlInputFile is not settable.");
145                         }
146                 }
147
148 #if NET_2_0
149                 protected internal
150 #else
151                 protected
152 #endif          
153                 override void OnPreRender (EventArgs e)
154                 {
155                         base.OnPreRender (e);
156
157                         if (Page != null && !Disabled) {
158                                 Page.RegisterRequiresPostBack (this);
159 #if NET_2_0
160                                 Page.RegisterEnabledControl (this);
161 #endif
162                         }
163                         
164                         HtmlForm form = (HtmlForm) SearchParentByType (typeof (HtmlForm));
165                         if (form != null && form.Enctype == "")
166                                 form.Enctype = "multipart/form-data";
167                 }
168
169                 Control SearchParentByType (Type type)
170                 {
171                         Control ctrl = Parent;
172                         while (ctrl != null) {
173                                 if (type.IsAssignableFrom (ctrl.GetType ())) {
174                                         return ctrl;
175                                 }
176                                 ctrl = ctrl.Parent;
177                         }
178
179                         return null;
180                 }
181
182                 bool LoadPostDataInternal (string postDataKey, NameValueCollection postCollection)
183                 {
184                         posted_file = Page.Request.Files [postDataKey];
185                         
186                         return (false);
187                 }
188
189                 void RaisePostDataChangedEventInternal ()
190                 {
191                         /* No events to raise */
192                 }
193
194 #if NET_2_0
195                 protected virtual bool LoadPostData (string postDataKey, NameValueCollection postCollection)
196                 {
197                         return LoadPostDataInternal (postDataKey, postCollection);
198                 }
199
200                 protected virtual void RaisePostDataChangedEvent ()
201                 {
202                         RaisePostDataChangedEventInternal ();
203                 }
204 #endif          
205                 
206                 bool IPostBackDataHandler.LoadPostData (string postDataKey, NameValueCollection postCollection)
207                 {
208 #if NET_2_0
209                         return LoadPostData (postDataKey, postCollection);
210 #else
211                         return LoadPostDataInternal (postDataKey, postCollection);
212 #endif
213                 }
214
215                 void IPostBackDataHandler.RaisePostDataChangedEvent ()
216                 {
217 #if NET_2_0
218                         RaisePostDataChangedEvent ();
219 #else
220                         RaisePostDataChangedEventInternal ();
221 #endif
222                 }
223         }
224 }
225
226