HtmlInputFile.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. //
  2. // System.Web.UI.HtmlControls.HtmlInputFile.cs
  3. //
  4. // Author:
  5. // Dick Porter <[email protected]>
  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. using System.Collections.Specialized;
  29. using System.ComponentModel;
  30. using System.Security.Permissions;
  31. namespace System.Web.UI.HtmlControls
  32. {
  33. // CAS
  34. [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  35. [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  36. // attributes
  37. [ValidationProperty ("Value")]
  38. public class HtmlInputFile : HtmlInputControl , IPostBackDataHandler
  39. {
  40. public HtmlInputFile () : base ("file")
  41. {
  42. }
  43. [DefaultValue ("")]
  44. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  45. [WebSysDescription("")]
  46. public string Accept
  47. {
  48. get {
  49. string acc = Attributes["accept"];
  50. if (acc == null) {
  51. return (String.Empty);
  52. }
  53. return (acc);
  54. }
  55. set {
  56. if (value == null) {
  57. Attributes.Remove ("accept");
  58. } else {
  59. Attributes["accept"] = value;
  60. }
  61. }
  62. }
  63. [DefaultValue ("")]
  64. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  65. [WebSysDescription("")]
  66. public int MaxLength
  67. {
  68. get {
  69. string maxlen = Attributes["maxlength"];
  70. if (maxlen == null) {
  71. return (-1);
  72. } else {
  73. return (Convert.ToInt32 (maxlen));
  74. }
  75. }
  76. set {
  77. if (value == -1) {
  78. Attributes.Remove ("maxlength");
  79. } else {
  80. Attributes["maxlength"] = value.ToString ();
  81. }
  82. }
  83. }
  84. HttpPostedFile posted_file;
  85. [DefaultValue ("")]
  86. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  87. [WebSysDescription("")]
  88. [WebCategory("Misc")]
  89. public HttpPostedFile PostedFile
  90. {
  91. get {
  92. return (posted_file);
  93. }
  94. }
  95. #if NET_2_0
  96. [DefaultValue ("-1")]
  97. #else
  98. [DefaultValue ("")]
  99. #endif
  100. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  101. [WebSysDescription("")]
  102. [WebCategory("Appearance")]
  103. public int Size
  104. {
  105. get {
  106. string size = Attributes["size"];
  107. if (size == null) {
  108. return (-1);
  109. } else {
  110. return (Convert.ToInt32 (size));
  111. }
  112. }
  113. set {
  114. if (value == -1) {
  115. Attributes.Remove ("size");
  116. } else {
  117. Attributes["size"] = value.ToString ();
  118. }
  119. }
  120. }
  121. [Browsable (false)]
  122. public override string Value
  123. {
  124. get {
  125. HttpPostedFile file = PostedFile;
  126. if (file == null)
  127. return string.Empty;
  128. return file.FileName;
  129. }
  130. set {
  131. throw new NotSupportedException ("The value property on HtmlInputFile is not settable.");
  132. }
  133. }
  134. #if NET_2_0
  135. protected internal
  136. #else
  137. protected
  138. #endif
  139. override void OnPreRender (EventArgs e)
  140. {
  141. base.OnPreRender (e);
  142. if (Page != null && !Disabled) {
  143. Page.RegisterRequiresPostBack (this);
  144. #if NET_2_0
  145. Page.RegisterEnabledControl (this);
  146. #endif
  147. }
  148. HtmlForm form = (HtmlForm) SearchParentByType (typeof (HtmlForm));
  149. if (form != null && form.Enctype == "")
  150. form.Enctype = "multipart/form-data";
  151. }
  152. Control SearchParentByType (Type type)
  153. {
  154. Control ctrl = Parent;
  155. while (ctrl != null) {
  156. if (type.IsAssignableFrom (ctrl.GetType ())) {
  157. return ctrl;
  158. }
  159. ctrl = ctrl.Parent;
  160. }
  161. return null;
  162. }
  163. bool LoadPostDataInternal (string postDataKey, NameValueCollection postCollection)
  164. {
  165. posted_file = Page.Request.Files [postDataKey];
  166. return (false);
  167. }
  168. void RaisePostDataChangedEventInternal ()
  169. {
  170. /* No events to raise */
  171. }
  172. #if NET_2_0
  173. protected virtual bool LoadPostData (string postDataKey, NameValueCollection postCollection)
  174. {
  175. return LoadPostDataInternal (postDataKey, postCollection);
  176. }
  177. protected virtual void RaisePostDataChangedEvent ()
  178. {
  179. RaisePostDataChangedEventInternal ();
  180. }
  181. #endif
  182. bool IPostBackDataHandler.LoadPostData (string postDataKey, NameValueCollection postCollection)
  183. {
  184. #if NET_2_0
  185. return LoadPostData (postDataKey, postCollection);
  186. #else
  187. return LoadPostDataInternal (postDataKey, postCollection);
  188. #endif
  189. }
  190. void IPostBackDataHandler.RaisePostDataChangedEvent ()
  191. {
  192. #if NET_2_0
  193. RaisePostDataChangedEvent ();
  194. #else
  195. RaisePostDataChangedEventInternal ();
  196. #endif
  197. }
  198. }
  199. }