FileUpload 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
显示一个文本框控件和一个浏览按钮,使用户可以选择要上载到服务器的文件。
public ref class FileUpload : System::Web::UI::WebControls::WebControl
[System.Web.UI.ControlValueProperty("FileBytes")]
[System.Web.UI.ValidationProperty("FileName")]
public class FileUpload : System.Web.UI.WebControls.WebControl
[<System.Web.UI.ControlValueProperty("FileBytes")>]
[<System.Web.UI.ValidationProperty("FileName")>]
type FileUpload = class
inherit WebControl
Public Class FileUpload
Inherits WebControl
- 继承
- 属性
示例
本主题附带了一个包含源代码的 Visual Studio 网站项目: 下载。
本部分包含以下四个示例:
第一个示例演示如何创建一个 FileUpload 控件,用于将文件保存到代码中指定的路径。
第二个示例演示如何创建一个 FileUpload 控件,用于将文件保存到应用程序的文件系统中的指定目录。
第三个示例演示如何创建一个 FileUpload 控件,用于将文件保存到指定路径并限制可上传的文件的大小。
第四个示例演示如何创建一个 FileUpload 控件,用于将文件保存到指定路径,并且仅允许上传具有 .doc 或 .xls 文件扩展名的文件。
注意
这些示例演示了控件的基本语法 FileUpload ,但它们并未演示保存文件之前应完成的所有必要错误检查。 有关更完整的示例,请参见 SaveAs。
以下示例演示如何创建一个 FileUpload 控件,用于将文件保存到代码中指定的路径。 SaveAs调用 方法将文件保存到服务器上的指定路径。
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void UploadButton_Click(object sender, EventArgs e)
{
// Specify the path on the server to
// save the uploaded file to.
String savePath = @"c:\temp\uploads\";
// Before attempting to perform operations
// on the file, verify that the FileUpload
// control contains a file.
if (FileUpload1.HasFile)
{
// Get the name of the file to upload.
String fileName = FileUpload1.FileName;
// Append the name of the file to upload to the path.
savePath += fileName;
// Call the SaveAs method to save the
// uploaded file to the specified path.
// This example does not perform all
// the necessary error checking.
// If a file with the same name
// already exists in the specified path,
// the uploaded file overwrites it.
FileUpload1.SaveAs(savePath);
// Notify the user of the name of the file
// was saved under.
UploadStatusLabel.Text = "Your file was saved as " + fileName;
}
else
{
// Notify the user that a file was not uploaded.
UploadStatusLabel.Text = "You did not specify a file to upload.";
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>FileUpload 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>
<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Sub UploadButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)
' Specify the path on the server to
' save the uploaded file to.
Dim savePath As String = "c:\temp\uploads\"
' Before attempting to perform operations
' on the file, verify that the FileUpload
' control contains a file.
If (FileUpload1.HasFile) Then
' Get the name of the file to upload.
Dim fileName As String = FileUpload1.FileName
' Append the name of the file to upload to the path.
savePath += fileName
' Call the SaveAs method to save the
' uploaded file to the specified path.
' This example does not perform all
' the necessary error checking.
' If a file with the same name
' already exists in the specified path,
' the uploaded file overwrites it.
FileUpload1.SaveAs(savePath)
' Notify the user of the name the file
' was saved under.
UploadStatusLabel.Text = "Your file was saved as " & fileName
Else
' Notify the user that a file was not uploaded.
UploadStatusLabel.Text = "You did not specify a file to upload."
End If
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>FileUpload 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>
以下示例演示如何创建一个 FileUpload 控件,用于将文件保存到应用程序的文件系统中的指定目录。 属性 HttpRequest.PhysicalApplicationPath 用于获取当前正在执行的服务器应用程序的根目录的物理文件系统路径。 SaveAs调用 方法将文件保存到服务器上的指定路径。
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void UploadButton_Click(object sender, EventArgs e)
{
// Save the uploaded file to an "Uploads" directory
// that already exists in the file system of the
// currently executing ASP.NET application.
// Creating an "Uploads" directory isolates uploaded
// files in a separate directory. This helps prevent
// users from overwriting existing application files by
// uploading files with names like "Web.config".
string saveDir = @"\Uploads\";
// Get the physical file system path for the currently
// executing application.
string appPath = Request.PhysicalApplicationPath;
// Before attempting to save the file, verify
// that the FileUpload control contains a file.
if (FileUpload1.HasFile)
{
string savePath = appPath + saveDir +
Server.HtmlEncode(FileUpload1.FileName);
// Call the SaveAs method to save the
// uploaded file to the specified path.
// This example does not perform all
// the necessary error checking.
// If a file with the same name
// already exists in the specified path,
// the uploaded file overwrites it.
FileUpload1.SaveAs(savePath);
// Notify the user that the file was uploaded successfully.
UploadStatusLabel.Text = "Your file was uploaded successfully.";
}
else
{
// Notify the user that a file was not uploaded.
UploadStatusLabel.Text = "You did not specify a file to upload.";
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>FileUpload Class Example</title>
</head>
<body>
<h3>FileUpload Class Example: Save To Application 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>
<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Sub UploadButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)
' Save the uploaded file to an "Uploads" directory
' that already exists in the file system of the
' currently executing ASP.NET application.
' Creating an "Uploads" directory isolates uploaded
' files in a separate directory. This helps prevent
' users from overwriting existing application files by
' uploading files with names like "Web.config".
Dim saveDir As String = "\Uploads\"
' Get the physical file system path for the currently
' executing application.
Dim appPath As String = Request.PhysicalApplicationPath
' Before attempting to save the file, verify
' that the FileUpload control contains a file.
If (FileUpload1.HasFile) Then
Dim savePath As String = appPath + saveDir + _
Server.HtmlEncode(FileUpload1.FileName)
' Call the SaveAs method to save the
' uploaded file to the specified path.
' This example does not perform all
' the necessary error checking.
' If a file with the same name
' already exists in the specified path,
' the uploaded file overwrites it.
FileUpload1.SaveAs(savePath)
' Notify the user that the file was uploaded successfully.
UploadStatusLabel.Text = "Your file was uploaded successfully."
Else
' Notify the user that a file was not uploaded.
UploadStatusLabel.Text = "You did not specify a file to upload."
End If
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>FileUpload Class Example</title>
</head>
<body>
<h3>FileUpload Class Example: Save To Application 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>
以下示例演示如何创建一个 FileUpload 控件,用于将文件保存到代码中指定的路径。 控件将可上载的文件的大小限制为 2 MB。 属性 PostedFile 用于访问基础 ContentLength 属性并返回文件的大小。 如果要上传的文件的大小小于 2 MB, SaveAs 则调用 方法将文件保存到服务器上的指定路径。 除了在应用程序代码中检查最大文件大小设置外,还可以将 httpRuntime 元素的 属性设置为maxRequestLength
应用程序的配置文件中允许的最大大小。
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void UploadButton_Click(object sender, EventArgs e)
{
// Specify the path on the server to
// save the uploaded file to.
string savePath = @"c:\temp\uploads\";
// Before attempting to save the file, verify
// that the FileUpload control contains a file.
if (FileUpload1.HasFile)
{
// Get the size in bytes of the file to upload.
int fileSize = FileUpload1.PostedFile.ContentLength;
// Allow only files less than 2,100,000 bytes (approximately 2 MB) to be uploaded.
if (fileSize < 2100000)
{
// Append the name of the uploaded file to the path.
savePath += Server.HtmlEncode(FileUpload1.FileName);
// Call the SaveAs method to save the
// uploaded file to the specified path.
// This example does not perform all
// the necessary error checking.
// If a file with the same name
// already exists in the specified path,
// the uploaded file overwrites it.
FileUpload1.SaveAs(savePath);
// Notify the user that the file was uploaded successfully.
UploadStatusLabel.Text = "Your file was uploaded successfully.";
}
else
{
// Notify the user why their file was not uploaded.
UploadStatusLabel.Text = "Your file was not uploaded because " +
"it exceeds the 2 MB size limit.";
}
}
else
{
// Notify the user that a file was not uploaded.
UploadStatusLabel.Text = "You did not specify a file to upload.";
}
}
</script>
<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>
<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Protected Sub UploadButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)
' Specify the path on the server to
' save the uploaded file to.
Dim savePath As String = "c:\temp\uploads\"
' Before attempting to save the file, verify
' that the FileUpload control contains a file.
If (FileUpload1.HasFile) Then
' Get the size in bytes of the file to upload.
Dim fileSize As Integer = FileUpload1.PostedFile.ContentLength
' Allow only files less than 2,100,000 bytes (approximately 2 MB) to be uploaded.
If (fileSize < 2100000) Then
' Append the name of the uploaded file to the path.
savePath += Server.HtmlEncode(FileUpload1.FileName)
' Call the SaveAs method to save the
' uploaded file to the specified path.
' This example does not perform all
' the necessary error checking.
' If a file with the same name
' already exists in the specified path,
' the uploaded file overwrites it.
FileUpload1.SaveAs(savePath)
' Notify the user that the file was uploaded successfully.
UploadStatusLabel.Text = "Your file was uploaded successfully."
Else
' Notify the user why their file was not uploaded.
UploadStatusLabel.Text = "Your file was not uploaded because " + _
"it exceeds the 2 MB size limit."
End If
Else
' Notify the user that a file was not uploaded.
UploadStatusLabel.Text = "You did not specify a file to upload."
End If
End Sub
</script>
<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>
以下示例演示如何创建一个 FileUpload 控件,用于将文件保存到代码中指定的路径。 此示例仅允许上传扩展名为 .doc 或 .xls 的文件。 Path.GetExtension调用 方法可返回要上传的文件的扩展名。 如果文件具有 .doc 或 .xls 文件扩展名, SaveAs 则调用 方法将文件保存到服务器上的指定路径。
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void UploadBtn_Click(object sender, EventArgs e)
{
// Specify the path on the server to
// save the uploaded file to.
string savePath = @"c:\temp\uploads";
// Before attempting to save the file, verify
// that the FileUpload control contains a file.
if (FileUpload1.HasFile)
{
// Get the name of the file to upload.
string fileName = Server.HtmlEncode(FileUpload1.FileName);
// Get the extension of the uploaded file.
string extension = System.IO.Path.GetExtension(fileName);
// Allow only files with .doc or .xls extensions
// to be uploaded.
if ((extension == ".doc") || (extension == ".xls"))
{
// Append the name of the file to upload to the path.
savePath += fileName;
// Call the SaveAs method to save the
// uploaded file to the specified path.
// This example does not perform all
// the necessary error checking.
// If a file with the same name
// already exists in the specified path,
// the uploaded file overwrites it.
FileUpload1.SaveAs(savePath);
// Notify the user that their file was successfully uploaded.
UploadStatusLabel.Text = "Your file was uploaded successfully.";
}
else
{
// Notify the user why their file was not uploaded.
UploadStatusLabel.Text = "Your file was not uploaded because " +
"it does not have a .doc or .xls extension.";
}
}
}
</script>
<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="UploadBtn"
Text="Upload file"
OnClick="UploadBtn_Click"
runat="server">
</asp:Button>
<hr />
<asp:Label id="UploadStatusLabel"
runat="server">
</asp:Label>
</div>
</form>
</body>
</html>
<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Protected Sub UploadBtn_Click(ByVal sender As Object, ByVal e As System.EventArgs)
' Specify the path on the server to
' save the uploaded file to.
Dim savePath As String = "c:\temp\uploads\"
' Before attempting to save the file, verify
' that the FileUpload control contains a file.
If (FileUpload1.HasFile) Then
' Get the name of the file to upload.
Dim fileName As String = Server.HtmlEncode(FileUpload1.FileName)
' Get the extension of the uploaded file.
Dim extension As String = System.IO.Path.GetExtension(fileName)
' Allow only files with .doc or .xls extensions
' to be uploaded.
If (extension = ".doc") Or (extension = ".xls") Then
' Append the name of the file to upload to the path.
savePath += fileName
' Call the SaveAs method to save the
' uploaded file to the specified path.
' This example does not perform all
' the necessary error checking.
' If a file with the same name
' already exists in the specified path,
' the uploaded file overwrites it.
FileUpload1.SaveAs(savePath)
' Notify the user that their file was successfully uploaded.
UploadStatusLabel.Text = "Your file was uploaded successfully."
Else
' Notify the user why their file was not uploaded.
UploadStatusLabel.Text = "Your file was not uploaded because " + _
"it does not have a .doc or .xls extension."
End If
Else
' Notify the user that a file was not uploaded.
UploadStatusLabel.Text = "You did not specify a file to upload."
End If
End Sub
</script>
<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="UploadBtn"
Text="Upload file"
OnClick="UploadBtn_Click"
runat="server">
</asp:Button>
<hr />
<asp:Label id="UploadStatusLabel"
runat="server">
</asp:Label>
</div>
</form>
</body>
</html>
注解
本主题内容:
介绍
类 FileUpload 显示一个文本框控件和一个浏览按钮,使用户能够选择客户端上的文件并将其上传到 Web 服务器。 用户通过在本地计算机上输入文件的完整路径来指定要上传的文件 (例如,在控件的文本框中 C:\MyFiles\TestFile.txt) 。 或者,用户可以通过单击“ 浏览 ”按钮,然后在“选择文件”对话框中查找文件来选择 该文件 。
FileName使用 属性获取客户端上使用 控件上传FileUpload的文件的名称。 此属性返回的文件名不包括客户端上的文件路径。
属性 FileContent 获取指向 Stream 要上传的文件的 对象。 使用此属性以字节的形式访问文件的内容。 例如,可以使用 Stream 属性返回 FileContent 的 对象以字节的形式读取文件的内容,并将其存储在字节数组中。 或者,可以使用 FileBytes 属性检索文件中的所有字节。
属性 PostedFile 获取要上传的文件的基础 HttpPostedFile 对象。 可以使用此属性访问文件上的其他属性。 属性 ContentLength 获取文件的长度。 属性 ContentType 获取文件的 MIME 内容类型。 此外,可以使用 PostedFile 属性访问 FileName 属性、 InputStream 属性和 SaveAs 方法。 但是,相同的功能由 FileName 属性、 FileContent 属性和 SaveAs 方法提供。
保存上传的文件
用户选择要上传的文件后,控件 FileUpload 不会自动将文件保存到服务器。 必须显式提供控件或机制,以允许用户提交指定的文件。 例如,可以提供用户单击以上传文件的按钮。 为保存指定文件而编写的代码应调用 SaveAs 方法,该方法将文件的内容保存到服务器上的指定路径。 通常, SaveAs 在事件处理方法中调用 方法,该事件将引发发回服务器。 例如,如果提供用于提交文件的按钮,则可以包含用于将文件保存在 click 事件的事件处理方法中的代码。
在调用 SaveAs 方法将文件保存到服务器之前,请使用 HasFile 属性验证控件 FileUpload 是否包含文件。
HasFile如果 返回 true
,则调用 SaveAs 方法。 如果返回 false
,则向用户显示一条消息,指示控件不包含文件。 不要检查 PostedFile 属性以确定是否存在要上传的文件,因为默认情况下,此属性包含 0 个字节。 因此,即使控件 FileUpload 为空,该属性也会 PostedFile 返回非 null 值。
安全注意事项
调用 SaveAs 方法时,必须指定要在其中保存上传文件的目录的完整路径。 如果未在应用程序代码中显式指定路径,当用户尝试上传文件时,将引发异常。 此行为通过阻止用户写入应用程序目录结构中的任意位置以及阻止访问敏感根目录,帮助保护服务器上的文件安全。
方法 SaveAs 将上传的文件写入指定的目录。 因此,ASP.NET 应用程序必须对服务器上的目录具有写入访问权限。 应用程序可通过两种方式获取写入访问权限。 可以在保存上传文件的目录中显式授予对运行应用程序的帐户的写入访问权限。 或者,可以提高授予 ASP.NET 应用程序的信任级别。 若要获取对应用程序的执行目录的写入访问权限,必须将信任级别设置为 AspNetHostingPermissionLevel.Medium 值的对象授予AspNetHostingPermission应用程序。 提高信任级别会增加应用程序对服务器上的资源的访问权限。 请注意,这不是一种安全的方法,因为获得应用程序控制权的恶意用户也将能够在这种更高的信任级别下运行。 最佳做法是在用户的上下文中运行 ASP.NET 应用程序,具有运行应用程序所需的最低权限。 有关 ASP.NET 应用程序中的安全性的详细信息,请参阅 Web 应用程序和 ASP.NET 信任级别和策略文件的基本安全做法。
内存限制
防止拒绝服务攻击的一种方法是限制可以使用 FileUpload 控件上传的文件的大小。 应设置适合预期上传的文件类型的大小限制。 默认大小限制为 4096 KB) (KB,) 为 4 MB (MB。 可以通过设置 maxRequestLength
httpRuntime 元素的 属性来允许上传较大的文件。 若要增加整个应用程序允许的最大文件大小,请在 Web.config 文件中设置 maxRequestLength
属性。 若要增加指定页面允许的最大文件大小,请在 Web.config 中的 元素内location
设置 maxRequestLength
属性。有关示例,请参阅 location Element (ASP.NET Settings Schema) 。
上传大型文件时,用户还可能收到以下错误消息:
aspnet_wp.exe (PID: 1520) was recycled because memory consumption exceeded 460 MB (60 percent of available RAM).
如果用户遇到此错误消息,请在应用程序的 Web.config 文件元素的 processModel 中增加 属性的值memoryLimit
。 属性 memoryLimit
指定工作进程可以使用的最大内存量。 如果工作进程超过数量 memoryLimit
,则会创建一个新进程来替换该进程,并将所有当前请求重新分配给新进程。
若要控制在处理请求时要上传的文件是暂时存储在内存中还是存储在服务器上,请设置 requestLengthDiskThreshold
httpRuntime 元素的 属性。 使用此属性可以管理输入流缓冲区的大小。 默认值为 256 字节。 指定的值不应超过为 maxRequestLength
属性指定的值。
将 FileUpload 控件与 UpdatePanel 控件配合使用
该 FileUpload 控件设计为仅在回发方案中使用,而不是在部分页面呈现期间的异步回发方案中使用。 在 FileUpload 控件中使用 UpdatePanel 控件时,必须使用作为 PostBackTrigger 面板对象的控件上传文件。 UpdatePanel 控件用于更新页面的选定区域,而不是使用回发更新整个页面。 有关详细信息,请参阅 UpdatePanel 控件概述 和 分页呈现概述。
声明性语法
<asp:FileUpload
AccessKey="string"
BackColor="color name|#dddddd"
BorderColor="color name|#dddddd"
BorderStyle="NotSet|None|Dotted|Dashed|Solid|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"
/>
构造函数
FileUpload() |
初始化 FileUpload 类的新实例。 |
属性
AccessKey |
获取或设置使您得以快速导航到 Web 服务器控件的访问键。 (继承自 WebControl) |
Adapter |
获取控件的浏览器特定适配器。 (继承自 Control) |
AllowMultiple |
获取或设置指定是否可选择多个文件用于上载的值。 |
AppRelativeTemplateSourceDirectory |
获取或设置包含该控件的 Page 或 UserControl 对象的应用程序相对虚拟目录。 (继承自 Control) |
Attributes |
获取与控件的特性不对应的任意特性(只用于呈现)的集合。 (继承自 WebControl) |
BackColor |
获取或设置 Web 服务器控件的背景色。 (继承自 WebControl) |
BindingContainer |
获取包含该控件的数据绑定的控件。 (继承自 Control) |
BorderColor |
获取或设置 Web 控件的边框颜色。 (继承自 WebControl) |
BorderStyle |
获取或设置 Web 服务器控件的边框样式。 (继承自 WebControl) |
BorderWidth |
获取或设置 Web 服务器控件的边框宽度。 (继承自 WebControl) |
ChildControlsCreated |
获取一个值,该值指示是否已创建服务器控件的子控件。 (继承自 Control) |
ClientID |
获取由 ASP.NET 生成的 HTML 标记的控件 ID。 (继承自 Control) |
ClientIDMode |
获取或设置用于生成 ClientID 属性值的算法。 (继承自 Control) |
ClientIDSeparator |
获取一个字符值,该值表示 ClientID 属性中使用的分隔符字符。 (继承自 Control) |
Context |
为当前 Web 请求获取与服务器控件关联的 HttpContext 对象。 (继承自 Control) |
Controls |
获取 ControlCollection 对象,该对象表示 UI 层次结构中的指定服务器控件的子控件。 (继承自 Control) |
ControlStyle |
获取 Web 服务器控件的样式。 此属性主要由控件开发人员使用。 (继承自 WebControl) |
ControlStyleCreated |
获取一个值,该值指示是否已为 Style 属性创建了 ControlStyle 对象。 此属性主要由控件开发人员使用。 (继承自 WebControl) |
CssClass |
获取或设置由 Web 服务器控件在客户端呈现的级联样式表 (CSS) 类。 (继承自 WebControl) |
DataItemContainer |
如果命名容器实现 IDataItemContainer,则获取对命名容器的引用。 (继承自 Control) |
DataKeysContainer |
如果命名容器实现 IDataKeysControl,则获取对命名容器的引用。 (继承自 Control) |
DesignMode |
获取一个值,该值指示是否正在使用设计图面上的一个控件。 (继承自 Control) |
Enabled |
获取或设置一个值,该值指示是否启用 Web 服务器控件。 (继承自 WebControl) |
EnableTheming |
获取或设置一个值,该值指示主题是否应用于该控件。 (继承自 WebControl) |
EnableViewState |
获取或设置一个值,该值指示服务器控件是否向发出请求的客户端保持自己的视图状态以及它所包含的任何子控件的视图状态。 (继承自 Control) |
Events |
获取控件的事件处理程序委托列表。 此属性为只读。 (继承自 Control) |
FileBytes |
从使用 FileUpload 控件指定的文件中获取一个字节数组。 |
FileContent |
获取 Stream 对象,它指向要使用 FileUpload 控件上载的文件。 |
FileName |
获取客户端上使用 FileUpload 控件上载的文件的名称。 |
Font |
获取与 Web 服务器控件关联的字体属性。 (继承自 WebControl) |
ForeColor |
获取或设置 Web 服务器控件的前景色(通常是文本颜色)。 (继承自 WebControl) |
HasAttributes |
获取一个值,该值指示控件是否具有特性集。 (继承自 WebControl) |
HasChildViewState |
获取一个值,该值指示当前服务器控件的子控件是否具有任何已保存的视图状态设置。 (继承自 Control) |
HasFile |
获取一个值,该值指示 FileUpload 控件是否包含文件。 |
HasFiles |
获取指示所有未见是否已经被上传的值。 |
Height |
获取或设置 Web 服务器控件的高度。 (继承自 WebControl) |
ID |
获取或设置分配给服务器控件的编程标识符。 (继承自 Control) |
IdSeparator |
获取用于分隔控件标识符的字符。 (继承自 Control) |
IsChildControlStateCleared |
获取一个值,该值指示该控件中包含的控件是否具有控件状态。 (继承自 Control) |
IsEnabled |
获取一个值,该值指示是否启用控件。 (继承自 WebControl) |
IsTrackingViewState |
获取一个值,用于指示服务器控件是否会将更改保存到其视图状态中。 (继承自 Control) |
IsViewStateEnabled |
获取一个值,该值指示是否为该控件启用了视图状态。 (继承自 Control) |
LoadViewStateByID |
获取一个值,该值指示控件是否通过 ID 而不是索引参与加载其视图状态。 (继承自 Control) |
NamingContainer |
获取对服务器控件的命名容器的引用,此引用创建唯一的命名空间,以区分具有相同 ID 属性值的服务器控件。 (继承自 Control) |
Page |
获取对包含服务器控件的 Page 实例的引用。 (继承自 Control) |
Parent |
获取对页 UI 层次结构中服务器控件的父控件的引用。 (继承自 Control) |
PostedFile |
获取使用 FileUpload 控件上载的文件的基础 HttpPostedFile 对象。 |
PostedFiles |
获取已上载文件的集合。 |
RenderingCompatibility |
获取一个值,该值指定呈现的 HTML 将与之兼容的 ASP.NET 版本。 (继承自 Control) |
Site |
获取容器信息,该容器在呈现于设计图面上时承载当前控件。 (继承自 Control) |
SkinID |
获取或设置要应用于控件的外观。 (继承自 WebControl) |
Style |
获取将在 Web 服务器控件的外部标记上呈现为样式特性的文本特性的集合。 (继承自 WebControl) |
SupportsDisabledAttribute |
获取一个值,该值指示在控件的 |
TabIndex |
获取或设置 Web 服务器控件的选项卡索引。 (继承自 WebControl) |
TagKey |
获取对应于此 Web 服务器控件的 HtmlTextWriterTag 值。 此属性主要由控件开发人员使用。 (继承自 WebControl) |
TagName |
获取控件标记的名称。 此属性主要由控件开发人员使用。 (继承自 WebControl) |
TemplateControl |
获取或设置对包含该控件的模板的引用。 (继承自 Control) |
TemplateSourceDirectory |
获取包含当前服务器控件的 Page 或 UserControl 的虚拟目录。 (继承自 Control) |
ToolTip |
获取或设置当鼠标指针悬停在 Web 服务器控件上时显示的文本。 (继承自 WebControl) |
UniqueID |
获取服务器控件的唯一的、以分层形式限定的标识符。 (继承自 Control) |
ValidateRequestMode |
获取或设置指示控件是否检查来自浏览器的客户端输入是否具有潜在危险值的值。 (继承自 Control) |
ViewState |
获取状态信息的字典,这些信息使您可以在同一页的多个请求间保存和还原服务器控件的视图状态。 (继承自 Control) |
ViewStateIgnoresCase |
获取一个值,该值指示 StateBag 对象是否不区分大小写。 (继承自 Control) |
ViewStateMode |
获取或设置此控件的视图状态模式。 (继承自 Control) |
Visible |
获取或设置一个值,该值指示服务器控件是否作为 UI 呈现在页上。 (继承自 Control) |
Width |
获取或设置 Web 服务器控件的宽度。 (继承自 WebControl) |
方法
事件
DataBinding |
当服务器控件绑定到数据源时发生。 (继承自 Control) |
Disposed |
当从内存释放服务器控件时发生,这是请求 ASP.NET 页时服务器控件生存期的最后阶段。 (继承自 Control) |
Init |
当服务器控件初始化时发生;初始化是控件生存期的第一步。 (继承自 Control) |
Load |
当服务器控件加载到 Page 对象中时发生。 (继承自 Control) |
PreRender |
在加载 Control 对象之后、呈现之前发生。 (继承自 Control) |
Unload |
当服务器控件从内存中卸载时发生。 (继承自 Control) |
显式接口实现
扩展方法
FindDataSourceControl(Control) |
返回与指定控件的数据控件关联的数据源。 |
FindFieldTemplate(Control, String) |
返回指定控件的命名容器中指定列的字段模板。 |
FindMetaTable(Control) |
返回包含数据控件的元表对象。 |