HttpFileCollection 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
提供对由客户端上载的文件的访问权限,并组织这些文件。
public ref class HttpFileCollection sealed : System::Collections::Specialized::NameObjectCollectionBase
public sealed class HttpFileCollection : System.Collections.Specialized.NameObjectCollectionBase
type HttpFileCollection = class
inherit NameObjectCollectionBase
Public NotInheritable Class HttpFileCollection
Inherits NameObjectCollectionBase
- 继承
示例
下面的示例演示如何访问 HttpFileCollection 从 Files 对象属性返回的 HttpRequest 集合。 集合 HttpFileCollection 由网页上的两个 FileUpload 控件填充。 文件集合中的项显示在控件中 BulletedList 。
<%@ 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 Button1_Click(object sender, EventArgs e)
{
// Clear the BulletedList.
BulletedList1.Items.Clear();
// Check to see if at least one file was specified.
if (FileUpload1.HasFile | FileUpload2.HasFile)
{
Label1.Text = "The file collection consists of:";
// Get the HttpFileCollection.
HttpFileCollection hfc = Request.Files;
foreach (String h in hfc.AllKeys)
{
// Add an item to the BulletedList if a file
// was specified for the corresponding control.
if (hfc[h].ContentLength > 0)
BulletedList1.Items.Add(Server.HtmlEncode(hfc[h].FileName));
}
}
else
{
Label1.Text = "You did not specify any files to upload or " +
"the file(s) could not be found.";
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>HttpFileCollection Example</title>
</head>
<body>
<form id="form1"
runat="server">
<div>
<asp:FileUpload ID="FileUpload1"
runat="server" />
<br />
<asp:FileUpload ID="FileUpload2"
runat="server" />
<br />
<asp:Button ID="Button1"
runat="server"
OnClick="Button1_Click"
Text="Upload" />
<br />
<asp:Label ID="Label1"
runat="server"/>
<br />
<asp:BulletedList ID="BulletedList1"
runat="server">
</asp:BulletedList>
</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 Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
' Clear the BulletedList.
BulletedList1.Items.Clear()
' Check to see if at least one file was specified.
If (FileUpload1.HasFile Or FileUpload2.HasFile) Then
Label1.Text = "The file collection consists of:"
' Get the HttpFileCollection.
Dim hfc As HttpFileCollection = Request.Files
For Each h As String In hfc.AllKeys
' Add an item to the BulletedList if a file
' was specified for the corresponding control.
If (hfc(h).ContentLength > 0) Then
BulletedList1.Items.Add(Server.HtmlEncode(hfc(h).FileName))
End If
Next
Else
Label1.Text = "You did not specify any files to upload or " & _
"the file(s) could not be found."
End If
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>HttpFileCollection Example</title>
</head>
<body>
<form id="form1"
runat="server">
<div>
<asp:FileUpload ID="FileUpload1"
runat="server" />
<br />
<asp:FileUpload ID="FileUpload2"
runat="server" />
<br />
<asp:Button ID="Button1"
runat="server"
OnClick="Button1_Click"
Text="Upload" />
<br />
<asp:Label ID="Label1"
runat="server"/>
<br />
<asp:BulletedList ID="BulletedList1"
runat="server">
</asp:BulletedList>
</div>
</form>
</body>
</html>
注解
客户端使用包含 HTTP Content-Type
标头 multipart/form-data
的多部分 MIME 格式在内容正文中编码文件并将其传输。 ASP.NET 将编码的文件从内容正文 () 提取到单个成员中HttpFileCollection。 类的方法和属性 HttpPostedFile 提供对每个文件的内容和属性的访问权限。
属性
AllKeys |
获取一个字符串数组,该数组包含文件集合中所有成员的键(名称)。 |
Count |
获取包含在 NameObjectCollectionBase 实例中的键/值对的数目。 (继承自 NameObjectCollectionBase) |
IsReadOnly |
获取或设置一个值,通过该值指示 NameObjectCollectionBase 实例是否为只读的。 (继承自 NameObjectCollectionBase) |
Item[Int32] |
从 HttpFileCollection 中获取具有指定数字索引的对象。 |
Item[String] |
从文件集合中获取具有指定名称的对象。 |
Keys |
获取包含 NameObjectCollectionBase.KeysCollection 实例中所有键的 NameObjectCollectionBase 实例。 (继承自 NameObjectCollectionBase) |
方法
显式接口实现
ICollection.CopyTo(Array, Int32) |
从目标数组的指定索引处开始将整个 NameObjectCollectionBase 复制到兼容的一维 Array。 (继承自 NameObjectCollectionBase) |
ICollection.IsSynchronized |
获取一个值,该值指示对 NameObjectCollectionBase 对象的访问是否同步(线程安全)。 (继承自 NameObjectCollectionBase) |
ICollection.SyncRoot |
获取一个对象,该对象可用于同步对 NameObjectCollectionBase 对象的访问。 (继承自 NameObjectCollectionBase) |
扩展方法
Cast<TResult>(IEnumerable) |
将 IEnumerable 的元素强制转换为指定的类型。 |
OfType<TResult>(IEnumerable) |
根据指定类型筛选 IEnumerable 的元素。 |
AsParallel(IEnumerable) |
启用查询的并行化。 |
AsQueryable(IEnumerable) |
将 IEnumerable 转换为 IQueryable。 |