HtmlInputFile.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. //
  2. // System.Web.UI.HtmlControls.HtmlInputFile.cs
  3. //
  4. // Author:
  5. // Dick Porter <[email protected]>
  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. 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. HttpPostedFile posted_file;
  41. public HtmlInputFile () : base ("file")
  42. {
  43. }
  44. [DefaultValue ("")]
  45. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  46. [WebSysDescription("")]
  47. public string Accept {
  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. get {
  68. string maxlen = Attributes["maxlength"];
  69. if (maxlen == null) {
  70. return (-1);
  71. } else {
  72. return (Convert.ToInt32 (maxlen));
  73. }
  74. }
  75. set {
  76. if (value == -1) {
  77. Attributes.Remove ("maxlength");
  78. } else {
  79. Attributes["maxlength"] = value.ToString ();
  80. }
  81. }
  82. }
  83. [DefaultValue ("")]
  84. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  85. [WebSysDescription("")]
  86. [WebCategory("Misc")]
  87. public HttpPostedFile PostedFile {
  88. get {
  89. return (posted_file);
  90. }
  91. }
  92. [DefaultValue ("-1")]
  93. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  94. [WebSysDescription("")]
  95. [WebCategory("Appearance")]
  96. public int Size {
  97. get {
  98. string size = Attributes["size"];
  99. if (size == null) {
  100. return (-1);
  101. } else {
  102. return (Convert.ToInt32 (size));
  103. }
  104. }
  105. set {
  106. if (value == -1) {
  107. Attributes.Remove ("size");
  108. } else {
  109. Attributes["size"] = value.ToString ();
  110. }
  111. }
  112. }
  113. [Browsable (false)]
  114. public override string Value {
  115. get {
  116. HttpPostedFile file = PostedFile;
  117. if (file == null)
  118. return string.Empty;
  119. return file.FileName;
  120. }
  121. set {
  122. throw new NotSupportedException ("The value property on HtmlInputFile is not settable.");
  123. }
  124. }
  125. protected internal override void OnPreRender (EventArgs e)
  126. {
  127. base.OnPreRender (e);
  128. Page page = Page;
  129. if (page != null && !Disabled) {
  130. page.RegisterRequiresPostBack (this);
  131. page.RegisterEnabledControl (this);
  132. }
  133. HtmlForm form = (HtmlForm) SearchParentByType (typeof (HtmlForm));
  134. if (form != null && form.Enctype == String.Empty)
  135. form.Enctype = "multipart/form-data";
  136. }
  137. Control SearchParentByType (Type type)
  138. {
  139. Control ctrl = Parent;
  140. while (ctrl != null) {
  141. if (type.IsAssignableFrom (ctrl.GetType ())) {
  142. return ctrl;
  143. }
  144. ctrl = ctrl.Parent;
  145. }
  146. return null;
  147. }
  148. bool LoadPostDataInternal (string postDataKey, NameValueCollection postCollection)
  149. {
  150. Page page = Page;
  151. if (page != null)
  152. posted_file = page.Request.Files [postDataKey];
  153. return (false);
  154. }
  155. void RaisePostDataChangedEventInternal ()
  156. {
  157. /* No events to raise */
  158. }
  159. protected virtual bool LoadPostData (string postDataKey, NameValueCollection postCollection)
  160. {
  161. return LoadPostDataInternal (postDataKey, postCollection);
  162. }
  163. protected virtual void RaisePostDataChangedEvent ()
  164. {
  165. RaisePostDataChangedEventInternal ();
  166. }
  167. bool IPostBackDataHandler.LoadPostData (string postDataKey, NameValueCollection postCollection)
  168. {
  169. return LoadPostData (postDataKey, postCollection);
  170. }
  171. void IPostBackDataHandler.RaisePostDataChangedEvent ()
  172. {
  173. RaisePostDataChangedEvent ();
  174. }
  175. }
  176. }