2 // (C) 2005 Mainsoft Corporation (http://www.mainsoft.com)
5 // Andrew Skiba <andrews@mainsoft.com>
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:
15 // The above copyright notice and this permission notice shall be
16 // included in all copies or substantial portions of the Software.
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.
29 using System.ComponentModel;
32 using System.Web.UI.HtmlControls;
33 using System.Security.Permissions;
36 namespace System.Web.UI.WebControls
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
46 : base (HtmlTextWriterTag.Input)
51 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
52 [Bindable (true, BindingDirection.OneWay)]
54 public byte[] FileBytes {
56 if (cachedBytes == null) {
57 cachedBytes = new byte[FileContent.Length];
58 FileContent.Read (cachedBytes, 0, cachedBytes.Length);
60 return (byte [])(cachedBytes.Clone ());
65 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
66 public Stream FileContent {
68 if (PostedFile == null)
71 return PostedFile.InputStream;
76 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
77 public string FileName {
79 if (PostedFile == null)
82 return PostedFile.FileName;
87 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
89 get { return PostedFile != null; }
93 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
94 public HttpPostedFile PostedFile {
96 if (Page == null || !Page.IsPostBack)
98 if (Context == null || Context.Request == null)
100 return Context.Request.Files[UniqueID];
104 protected override void AddAttributesToRender (HtmlTextWriter writer)
106 writer.AddAttribute (HtmlTextWriterAttribute.Type, "file");
107 if (!string.IsNullOrEmpty (UniqueID))
108 writer.AddAttribute (HtmlTextWriterAttribute.Name, UniqueID);
109 base.AddAttributesToRender (writer);
112 protected internal override void OnPreRender (System.EventArgs e)
114 base.OnPreRender (e);
115 Page.Form.Enctype = "multipart/form-data";
118 protected internal override void Render (HtmlTextWriter writer)
121 Page.VerifyRenderingInServerForm (this);
122 base.Render (writer);
125 public void SaveAs (string filename)
127 HttpPostedFile file = PostedFile;
129 file.SaveAs (filename);