Professional Documents
Culture Documents
WebC…
Home Library Learn Downloads Support Sign in | United States - English | Preferences
Remarks
In this topic:
Security Considerations
Memory Limitations
Declarative Syntax
Introduction
Before calling the SaveAs method to save the file to the server,
use the HasFile property to verify that the FileUpload control
contains a file. If the HasFile returns true, call the SaveAs method.
If it returns false, display a message to the user indicating that the
control does not contain a file. Do not check the PostedFile
property to determine whether a file to upload exists because, by
default, this property contains 0 bytes. As a result, even when the
FileUpload control is blank, the PostedFile property returns a non-
null value.
Security Considerations
When you call the SaveAs method, you must specify the full path
of the directory in which to save the uploaded file. If you do not
explicitly specify a path in your application code, an exception is
thrown when a user attempts to upload a file. This behavior helps
keep the files on the server secure by preventing users from
being able to write to arbitrary locations in your application's
directory structure, as well as preventing access to sensitive root
directories.
Memory Limitations
…microsoft.com/…/system.web.ui.webco… 3/14
4/28/2010 FileUpload Class (System.Web.UI.WebC…
Using the FileUpload Control with the UpdatePanel
Control
Declarative Syntax
Copy
<asp:FileUpload
AccessKey="string"
BackColor="color name|#dddddd"
BorderColor="color name|#dddddd"
BorderStyle="NotSet|None|Dotted|Dashed|Soli
d|Double|Groove|Ridge|
Inset|Outset"
BorderWidth="size"
CssClass="string"
Enabled="True|False"
EnableTheming="True|False"
EnableViewState="True|False"
Font-Bold="True|False"
Font-Italic="True|False"
Font-Names="string"
Font-Overline="True|False"
Font-Size="string|Smaller|Larger|XX-Small|X
-Small|Small|Medium|
Large|X-Large|XX-Large"
Font-Strikeout="True|False"
Font-Underline="True|False"
ForeColor="color name|#dddddd"
Height="size"
ID="string"
OnDataBinding="DataBinding event handler"
OnDisposed="Disposed event handler"
OnInit="Init event handler"
OnLoad="Load event handler"
OnPreRender="PreRender event handler"
OnUnload="Unload event handler"
runat="server"
SkinID="string"
Style="string"
TabIndex="integer"
ToolTip="string"
Visible="True|False"
Width="size"
/>
Topic Location
How to: Upload Files with the Building ASP .NET Web
…microsoft.com/…/system.web.ui.webco… 4/14
4/28/2010 FileUpload Class (System.Web.UI.WebC…
FileUpload Web Server Control Applications
How to: Upload Files with the Building ASP .NET Web
FileUpload Web Server Control Applications
How to: Upload Files with the Building ASP .NET Web
FileUpload Web Server Control Applications in Visual Studio
How to: Upload Files with the Building ASP .NET Web
FileUpload Web Server Control Applications in Visual Studio
Examples
This section contains the following four examples:
Caution
These examples demonstrate the basic syntax for the
FileUpload control, but they do not demonstrate all the
necessary error checking that should be completed before to
saving the file. For a more complete example, see SaveAs.
VB C# C++ F# JScript
Copy
…microsoft.com/…/system.web.ui.webco… 5/14
4/28/2010 FileUpload Class (System.Web.UI.WebC… Copy
<script runat="server">
}
</script>
…microsoft.com/…/system.web.ui.webco… 6/14
4/28/2010 FileUpload Class (System.Web.UI.WebC…
<body>
<form id="form1" runat="server">
<div>
<h4>Select a file to upload:</h4>
<asp:FileUpload id="FileUpload1"
runat="server">
</asp:FileUpload>
<asp:Button id="UploadButton"
Text="Upload file"
OnClick="UploadButton_Click"
runat="server">
</asp:Button>
<hr />
<asp:Label id="UploadStatusLabel"
runat="server">
</asp:Label>
</div>
</form>
</body>
</html>
VB C# C++ F# JScript
Copy
<script runat="server">
…microsoft.com/…/system.web.ui.webco… 7/14
4/28/2010 FileUpload Class (System.Web.UI.WebC…
// files in a separate directory. This
helps prevent
// users from overwriting existing appl
ication files by
// uploading files with names like "Web
.config".
string saveDir = @"\Uploads\";
}
else
{
// Notify the user that a file was
not uploaded.
UploadStatusLabel.Text = "You did n
ot specify a file to upload.";
}
}
</script>
…microsoft.com/…/system.web.ui.webco… 8/14
4/28/2010 FileUpload Class (System.Web.UI.WebC…
cation Directory</h3>
<form id="form1" runat="server">
<div>
<h4>Select a file to upload:</h4>
<asp:FileUpload id="FileUpload1"
runat="server">
</asp:FileUpload>
<br/><br/>
<asp:Button id="UploadButton"
Text="Upload file"
OnClick="UploadButton_Click"
runat="server">
</asp:Button>
<hr />
<asp:Label id="UploadStatusLabel"
runat="server">
</asp:Label>
</div>
</form>
</body>
</html>
VB C# C++ F# JScript
Copy
<script runat="server">
…microsoft.com/…/system.web.ui.webco… 9/14
4/28/2010 FileUpload Class (System.Web.UI.WebC…
// Before attempting to save the file,
verify
// that the FileUpload control contains
a file.
if (FileUpload1.HasFile)
{
// Get the size in bytes of the fil
e to upload.
int fileSize = FileUpload1.PostedFi
le.ContentLength;
…microsoft.com/…/system.web.ui.webco… 10/14
4/28/2010 FileUpload Class (System.Web.UI.WebC…
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>FileUpload Class Example</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h4>Select a file to upload:</h4>
<asp:FileUpload id="FileUpload1"
runat="server">
</asp:FileUpload>
<br/><br/>
<asp:Button id="UploadButton"
Text="Upload file"
OnClick="UploadButton_Click"
runat="server">
</asp:Button>
<hr />
<asp:Label id="UploadStatusLabel"
runat="server">
</asp:Label>
</div>
</form>
</body>
</html>
VB C# C++ F# JScript
Copy
<script runat="server">
…microsoft.com/…/system.web.ui.webco… 11/14
4/28/2010 FileUpload Class (System.Web.UI.WebC…
…microsoft.com/…/system.web.ui.webco… 12/14
4/28/2010 FileUpload Class (System.Web.UI.WebC…
</script>
<asp:FileUpload id="FileUpload1"
runat="server">
</asp:FileUpload>
<br/><br/>
<asp:Button id="UploadBtn"
Text="Upload file"
OnClick="UploadBtn_Click"
runat="server">
</asp:Button>
<hr />
<asp:Label id="UploadStatusLabel"
runat="server">
</asp:Label>
</div>
</form>
</body>
</html>
Inheritance Hierarchy
System.Object
System.Web.UI.Control
System.Web.UI.WebControls.WebControl
System.Web.UI.WebControls.FileUpload
Thread Safety
Any public static (Shared in Visual Basic) members of this type are
thread safe. Any instance members are not guaranteed to be
thread safe.
Platforms
Windows 7, Windows Vista, Windows XP SP3, Windows XP Media
Center Edition, Windows XP Professional x64 Edition, Windows XP
Starter Edition, Windows Server 2008, Windows Server 2003,
Windows Server 2000 SP4, Windows Millennium Edition, Windows
98
…microsoft.com/…/system.web.ui.webco… 13/14
4/28/2010 FileUpload Class (System.Web.UI.WebC…
The .NET Framework and .NET Compact Framework do not
support all versions of every platform. For a list of the supported
versions, see http://msdn.microsoft.com/en-
us/library/8z6watww.aspx.
Version Information
.NET Framework
See Also
Reference
FileUpload Members
System.Web.UI.WebControls Namespace
SaveAs
Community Content
© 2010 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement | Feedback
…microsoft.com/…/system.web.ui.webco… 14/14