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>
備註
用戶端會編碼檔案,並使用多部分 MIME 格式與 Content-Type
HTTP 標頭 multipart/form-data
在內容主體中傳輸檔案。 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。 |