Merge pull request #1458 from BrzVlad/feature-fat-cas
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / FileUpload.cs
1 //
2 // (C) 2005 Mainsoft Corporation (http://www.mainsoft.com)
3 //
4 // Authors:
5 //      Andrew Skiba <andrews@mainsoft.com>
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 using System;
27 using System.ComponentModel;
28 using System.Web;
29 using System.Web.UI;
30 using System.Web.UI.HtmlControls;
31 using System.Security.Permissions;
32 using System.IO;
33
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
48                 byte[] cachedBytes;
49                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
50                 [Bindable (true, BindingDirection.OneWay)]
51                 [Browsable (false)]
52                 public byte[] FileBytes {
53                         get {
54                                 if (cachedBytes == null) {
55                                         cachedBytes = new byte[FileContent.Length];
56                                         FileContent.Read (cachedBytes, 0, cachedBytes.Length);
57                                 }
58                                 return (byte [])(cachedBytes.Clone ());
59                         }
60                 }
61
62                 [Browsable(false)]
63                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
64                 public Stream FileContent {
65                         get {
66                                 if (PostedFile == null)
67                                         return Stream.Null;
68                                 else {
69                                         Stream ret = PostedFile.InputStream;
70                                         if (ret != null)
71                                                 ret.Position = 0;
72                                         
73                                         return ret;
74                                 }
75                         }
76                 }
77
78                 [Browsable (false)]
79                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
80                 public string FileName {
81                         get {
82                                 if (PostedFile == null)
83                                         return String.Empty;
84                                 else
85                                         return Path.GetFileName (PostedFile.FileName);
86                         }
87                 }
88
89                 [Browsable (false)]
90                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
91                 public bool HasFile {
92                         get {
93                                 HttpPostedFile pf = PostedFile;
94                                 return (pf != null && !String.IsNullOrEmpty (pf.FileName));
95                         }
96                 }
97
98                 [Browsable (false)]
99                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
100                 public HttpPostedFile PostedFile {
101                         get {
102                                 Page page = Page;
103                                 if (page == null || !page.IsPostBack)
104                                         return null;
105                                 if (Context == null || Context.Request == null)
106                                         return null;
107                                 return Context.Request.Files[UniqueID];
108                         }
109                 }
110
111                 protected override void AddAttributesToRender (HtmlTextWriter writer)
112                 {
113                         writer.AddAttribute (HtmlTextWriterAttribute.Type, "file", false);
114                         if (!string.IsNullOrEmpty (UniqueID))
115                                 writer.AddAttribute (HtmlTextWriterAttribute.Name, UniqueID);
116                         base.AddAttributesToRender (writer);
117                 }
118
119                 protected internal override void OnPreRender (System.EventArgs e)
120                 {
121                         base.OnPreRender (e);
122                         Page page = Page;
123                         if (page != null)
124                                 page.Form.Enctype = "multipart/form-data";
125                 }
126
127                 protected internal override void Render (HtmlTextWriter writer)
128                 {
129                         Page page = Page;
130                         if (page != null)
131                                 page.VerifyRenderingInServerForm (this);
132                         base.Render (writer);
133                 }
134
135                 public void SaveAs (string filename)
136                 {
137                         HttpPostedFile file = PostedFile;
138                         if (file != null)
139                                 file.SaveAs (filename);
140                 }
141         }
142 }
143