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