2 // System.Web.UI.HtmlControls.HtmlInputFile.cs
5 // Dick Porter <dick@ximian.com>
7 // Copyright (C) 2005-2010 Novell, Inc (http://www.novell.com)
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
29 using System.Collections.Specialized;
30 using System.ComponentModel;
31 using System.Security.Permissions;
33 namespace System.Web.UI.HtmlControls
36 [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
37 [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
39 [ValidationProperty ("Value")]
40 public class HtmlInputFile : HtmlInputControl , IPostBackDataHandler
42 HttpPostedFile posted_file;
44 public HtmlInputFile () : base ("file")
49 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
50 [WebSysDescription("")]
51 public string Accept {
53 string acc = Attributes["accept"];
56 return (String.Empty);
63 Attributes.Remove ("accept");
65 Attributes["accept"] = value;
71 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
72 [WebSysDescription("")]
73 public int MaxLength {
75 string maxlen = Attributes["maxlength"];
80 return (Convert.ToInt32 (maxlen));
85 Attributes.Remove ("maxlength");
87 Attributes["maxlength"] = value.ToString ();
93 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
94 [WebSysDescription("")]
96 public HttpPostedFile PostedFile {
102 [DefaultValue ("-1")]
103 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
104 [WebSysDescription("")]
105 [WebCategory("Appearance")]
108 string size = Attributes["size"];
113 return (Convert.ToInt32 (size));
118 Attributes.Remove ("size");
120 Attributes["size"] = value.ToString ();
126 public override string Value {
128 HttpPostedFile file = PostedFile;
132 return file.FileName;
135 throw new NotSupportedException ("The value property on HtmlInputFile is not settable.");
139 protected internal override void OnPreRender (EventArgs e)
141 base.OnPreRender (e);
144 if (page != null && !Disabled) {
145 page.RegisterRequiresPostBack (this);
146 page.RegisterEnabledControl (this);
149 HtmlForm form = (HtmlForm) SearchParentByType (typeof (HtmlForm));
150 if (form != null && form.Enctype == String.Empty)
151 form.Enctype = "multipart/form-data";
154 Control SearchParentByType (Type type)
156 Control ctrl = Parent;
157 while (ctrl != null) {
158 if (type.IsAssignableFrom (ctrl.GetType ())) {
167 bool LoadPostDataInternal (string postDataKey, NameValueCollection postCollection)
171 posted_file = page.Request.Files [postDataKey];
176 void RaisePostDataChangedEventInternal ()
178 /* No events to raise */
181 protected virtual bool LoadPostData (string postDataKey, NameValueCollection postCollection)
183 return LoadPostDataInternal (postDataKey, postCollection);
186 protected virtual void RaisePostDataChangedEvent ()
188 RaisePostDataChangedEventInternal ();
191 bool IPostBackDataHandler.LoadPostData (string postDataKey, NameValueCollection postCollection)
193 return LoadPostData (postDataKey, postCollection);
196 void IPostBackDataHandler.RaisePostDataChangedEvent ()
198 RaisePostDataChangedEvent ();