Merge pull request #5714 from alexischr/update_bockbuild
[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-2010 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                 HttpPostedFile posted_file;
43
44                 public HtmlInputFile () : base ("file")
45                 {
46                 }
47
48                 [DefaultValue ("")]
49                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
50                 [WebSysDescription("")]
51                 public string Accept {
52                         get {
53                                 string acc = Attributes["accept"];
54
55                                 if (acc == null) {
56                                         return (String.Empty);
57                                 }
58
59                                 return (acc);
60                         }
61                         set {
62                                 if (value == null) {
63                                         Attributes.Remove ("accept");
64                                 } else {
65                                         Attributes["accept"] = value;
66                                 }
67                         }
68                 }
69
70                 [DefaultValue ("")]
71                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
72                 [WebSysDescription("")]
73                 public int MaxLength {
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                 [DefaultValue ("")]
93                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
94                 [WebSysDescription("")]
95                 [WebCategory("Misc")]
96                 public HttpPostedFile PostedFile {
97                         get {
98                                 return (posted_file);
99                         }
100                 }
101                 
102                 [DefaultValue ("-1")]
103                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
104                 [WebSysDescription("")]
105                 [WebCategory("Appearance")]
106                 public int Size {
107                         get {
108                                 string size = Attributes["size"];
109                                 
110                                 if (size == null) {
111                                         return (-1);
112                                 } else {
113                                         return (Convert.ToInt32 (size));
114                                 }
115                         }
116                         set {
117                                 if (value == -1) {
118                                         Attributes.Remove ("size");
119                                 } else {
120                                         Attributes["size"] = value.ToString ();
121                                 }
122                         }
123                 }
124                 
125                 [Browsable (false)]
126                 public override string Value {
127                         get {
128                                 HttpPostedFile file = PostedFile;
129                                 if (file == null)
130                                         return string.Empty;
131
132                                 return file.FileName;
133                         }
134                         set {
135                                 throw new NotSupportedException ("The value property on HtmlInputFile is not settable.");
136                         }
137                 }
138
139                 protected internal override void OnPreRender (EventArgs e)
140                 {
141                         base.OnPreRender (e);
142
143                         Page page = Page;
144                         if (page != null && !Disabled) {
145                                 page.RegisterRequiresPostBack (this);
146                                 page.RegisterEnabledControl (this);
147                         }
148                         
149                         HtmlForm form = (HtmlForm) SearchParentByType (typeof (HtmlForm));
150                         if (form != null && form.Enctype == String.Empty)
151                                 form.Enctype = "multipart/form-data";
152                 }
153
154                 Control SearchParentByType (Type type)
155                 {
156                         Control ctrl = Parent;
157                         while (ctrl != null) {
158                                 if (type.IsAssignableFrom (ctrl.GetType ())) {
159                                         return ctrl;
160                                 }
161                                 ctrl = ctrl.Parent;
162                         }
163
164                         return null;
165                 }
166
167                 bool LoadPostDataInternal (string postDataKey, NameValueCollection postCollection)
168                 {
169                         Page page = Page;
170                         if (page != null)
171                                 posted_file = page.Request.Files [postDataKey];
172                         
173                         return (false);
174                 }
175
176                 void RaisePostDataChangedEventInternal ()
177                 {
178                         /* No events to raise */
179                 }
180
181                 protected virtual bool LoadPostData (string postDataKey, NameValueCollection postCollection)
182                 {
183                         return LoadPostDataInternal (postDataKey, postCollection);
184                 }
185
186                 protected virtual void RaisePostDataChangedEvent ()
187                 {
188                         RaisePostDataChangedEventInternal ();
189                 }
190                 
191                 bool IPostBackDataHandler.LoadPostData (string postDataKey, NameValueCollection postCollection)
192                 {
193                         return LoadPostData (postDataKey, postCollection);
194                 }
195
196                 void IPostBackDataHandler.RaisePostDataChangedEvent ()
197                 {
198                         RaisePostDataChangedEvent ();
199                 }
200         }
201 }
202
203