2010-02-18 Marek Habersack <mhabersack@novell.com>
[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 #if NET_2_0
27
28 using System;
29 using System.ComponentModel;
30 using System.Web;
31 using System.Web.UI;
32 using System.Web.UI.HtmlControls;
33 using System.Security.Permissions;
34 using System.IO;
35
36 namespace System.Web.UI.WebControls
37 {
38         [ControlValueProperty ("FileBytes")]
39         [ValidationProperty ("FileName")]
40         [Designer ("DesignerBaseTypeNameSystem.ComponentModel.Design.IDesignerDesignerTypeNameSystem.Web.UI.Design.WebControls.PreviewControlDesigner, " + Consts.AssemblySystem_Design)]
41         [AspNetHostingPermission(SecurityAction.LinkDemand, Level=AspNetHostingPermissionLevel.Minimal, Unrestricted=false)]
42         [AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level=AspNetHostingPermissionLevel.Minimal, Unrestricted=false)]
43         public class FileUpload : WebControl
44         {
45                 public FileUpload ()
46                         : base (HtmlTextWriterTag.Input)
47                 {
48                 }
49
50                 byte[] cachedBytes;
51                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
52                 [Bindable (true, BindingDirection.OneWay)]
53                 [Browsable (false)]
54                 public byte[] FileBytes {
55                         get {
56                                 if (cachedBytes == null) {
57                                         cachedBytes = new byte[FileContent.Length];
58                                         FileContent.Read (cachedBytes, 0, cachedBytes.Length);
59                                 }
60                                 return (byte [])(cachedBytes.Clone ());
61                         }
62                 }
63
64                 [Browsable(false)]
65                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
66                 public Stream FileContent {
67                         get {
68                                 if (PostedFile == null)
69                                         return Stream.Null;
70                                 else {
71                                         Stream ret = PostedFile.InputStream;
72                                         if (ret != null)
73                                                 ret.Position = 0;
74                                         
75                                         return ret;
76                                 }
77                         }
78                 }
79
80                 [Browsable (false)]
81                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
82                 public string FileName {
83                         get {
84                                 if (PostedFile == null)
85                                         return string.Empty;
86                                 else
87                                         return Path.GetFileName (PostedFile.FileName);
88                         }
89                 }
90
91                 [Browsable (false)]
92                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
93                 public bool HasFile {
94                         get {
95                                 HttpPostedFile pf = PostedFile;
96                                 return (pf != null && !String.IsNullOrEmpty (pf.FileName));
97                         }
98                 }
99
100                 [Browsable (false)]
101                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
102                 public HttpPostedFile PostedFile {
103                         get {
104                                 if (Page == null || !Page.IsPostBack)
105                                         return null;
106                                 if (Context == null || Context.Request == null)
107                                         return null;
108                                 return Context.Request.Files[UniqueID];
109                         }
110                 }
111
112                 protected override void AddAttributesToRender (HtmlTextWriter writer)
113                 {
114                         writer.AddAttribute (HtmlTextWriterAttribute.Type, "file", false);
115                         if (!string.IsNullOrEmpty (UniqueID))
116                                 writer.AddAttribute (HtmlTextWriterAttribute.Name, UniqueID);
117                         base.AddAttributesToRender (writer);
118                 }
119
120                 protected internal override void OnPreRender (System.EventArgs e)
121                 {
122                         base.OnPreRender (e);
123                         Page.Form.Enctype = "multipart/form-data";
124                 }
125
126                 protected internal override void Render (HtmlTextWriter writer)
127                 {
128                         if (Page != null)
129                                 Page.VerifyRenderingInServerForm (this);
130                         base.Render (writer);
131                 }
132
133                 public void SaveAs (string filename)
134                 {
135                         HttpPostedFile file = PostedFile;
136                         if (file != null)
137                                 file.SaveAs (filename);
138                 }
139         }
140 }
141 #endif