FileUpload.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. //
  2. // (C) 2005 Mainsoft Corporation (http://www.mainsoft.com)
  3. //
  4. // Authors:
  5. // Andrew Skiba <[email protected]>
  6. //
  7. // Permission is hereby granted, free of charge, to any person obtaining
  8. // a copy of this software and associated documentation files (the
  9. // "Software"), to deal in the Software without restriction, including
  10. // without limitation the rights to use, copy, modify, merge, publish,
  11. // distribute, sublicense, and/or sell copies of the Software, and to
  12. // permit persons to whom the Software is furnished to do so, subject to
  13. // the following conditions:
  14. //
  15. // The above copyright notice and this permission notice shall be
  16. // included in all copies or substantial portions of the Software.
  17. //
  18. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  19. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  20. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  21. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  22. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  23. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  24. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  25. //
  26. #if NET_2_0
  27. using System;
  28. using System.ComponentModel;
  29. using System.Web;
  30. using System.Web.UI;
  31. using System.Web.UI.HtmlControls;
  32. using System.Security.Permissions;
  33. using System.IO;
  34. namespace System.Web.UI.WebControls
  35. {
  36. [ControlValueProperty ("FileBytes")]
  37. [ValidationProperty ("FileName")]
  38. [Designer ("DesignerBaseTypeNameSystem.ComponentModel.Design.IDesignerDesignerTypeNameSystem.Web.UI.Design.WebControls.PreviewControlDesigner, " + Consts.AssemblySystem_Design)]
  39. [AspNetHostingPermission(SecurityAction.LinkDemand, Level=AspNetHostingPermissionLevel.Minimal, Unrestricted=false)]
  40. [AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level=AspNetHostingPermissionLevel.Minimal, Unrestricted=false)]
  41. public class FileUpload : WebControl
  42. {
  43. public FileUpload ()
  44. : base (HtmlTextWriterTag.Input)
  45. {
  46. }
  47. byte[] cachedBytes;
  48. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  49. [Bindable (true, BindingDirection.OneWay)]
  50. [Browsable (false)]
  51. public byte[] FileBytes {
  52. get {
  53. if (cachedBytes == null) {
  54. cachedBytes = new byte[FileContent.Length];
  55. FileContent.Read (cachedBytes, 0, cachedBytes.Length);
  56. }
  57. return (byte [])(cachedBytes.Clone ());
  58. }
  59. }
  60. [Browsable(false)]
  61. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  62. public Stream FileContent {
  63. get {
  64. if (PostedFile == null)
  65. return Stream.Null;
  66. else
  67. return PostedFile.InputStream;
  68. }
  69. }
  70. [Browsable (false)]
  71. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  72. public string FileName {
  73. get {
  74. if (PostedFile == null)
  75. return string.Empty;
  76. else
  77. return PostedFile.FileName;
  78. }
  79. }
  80. [Browsable (false)]
  81. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  82. public bool HasFile {
  83. get { return PostedFile != null; }
  84. }
  85. [Browsable (false)]
  86. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  87. public HttpPostedFile PostedFile {
  88. get {
  89. if (Page == null || !Page.IsPostBack)
  90. return null;
  91. if (Context == null || Context.Request == null)
  92. return null;
  93. return Context.Request.Files[UniqueID];
  94. }
  95. }
  96. protected override void AddAttributesToRender (HtmlTextWriter writer)
  97. {
  98. writer.AddAttribute (HtmlTextWriterAttribute.Type, "file");
  99. if (!string.IsNullOrEmpty (UniqueID))
  100. writer.AddAttribute (HtmlTextWriterAttribute.Name, UniqueID);
  101. base.AddAttributesToRender (writer);
  102. }
  103. protected internal override void OnPreRender (System.EventArgs e)
  104. {
  105. base.OnPreRender (e);
  106. Page.Form.Enctype = "multipart/form-data";
  107. }
  108. protected internal override void Render (HtmlTextWriter writer)
  109. {
  110. if (Page != null)
  111. Page.VerifyRenderingInServerForm (this);
  112. base.Render (writer);
  113. }
  114. public void SaveAs (string filename)
  115. {
  116. HttpPostedFile file = PostedFile;
  117. if (file != null)
  118. file.SaveAs (filename);
  119. }
  120. }
  121. }
  122. #endif