HttpFileCollection 類別

定義

提供用戶端所上傳檔案的存取權,並進行組織。Provides access to and organizes files uploaded by a client.

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

範例

下列範例示範如何存取 HttpFileCollection 從物件的屬性傳回的集合 Files HttpRequestThe following example demonstrates how to access the HttpFileCollection collection returned from the Files property of the HttpRequest object. HttpFileCollection 集合會由網頁上的兩個 FileUpload 控制項填入。The HttpFileCollection collection is populated by two FileUpload controls on the Web page. 檔案集合中的專案會顯示在 BulletedList 控制項中。Items in the file collection are displayed in a BulletedList control.

<%@ 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 格式(具有的 HTTP 標頭)來編碼檔案,並在內容主體中傳送這些檔案 Content-Type multipart/form-dataClients encode files and transmit them in the content body using multipart MIME format with an HTTP Content-Type header of multipart/form-data. ASP.NET 會將內容主體中) 的編碼檔案 (,解壓縮至的個別成員 HttpFileCollectionASP.NET extracts the encoded file(s) from the content body into individual members of an HttpFileCollection. 類別的方法和屬性 HttpPostedFile 可讓您存取每個檔案的內容和屬性。Methods and properties of the HttpPostedFile class provide access to the contents and properties of each file.

屬性

AllKeys

取得字串陣列,包含檔案集合中所有成員的索引鍵 (名稱)。Gets a string array containing the keys (names) of all members in the file collection.

Count

取得 NameObjectCollectionBase 執行個體中包含的索引鍵/值組數目。Gets the number of key/value pairs contained in the NameObjectCollectionBase instance.

(繼承來源 NameObjectCollectionBase)
IsReadOnly

取得或設定值,表示 NameObjectCollectionBase 執行個體是否為唯讀。Gets or sets a value indicating whether the NameObjectCollectionBase instance is read-only.

(繼承來源 NameObjectCollectionBase)
Item[Int32]

以指定的數值索引從 HttpFileCollection 取得物件。Gets the object with the specified numerical index from the HttpFileCollection.

Item[String]

以指定的名稱從檔案集合取得物件。Gets the object with the specified name from the file collection.

Keys

取得 NameObjectCollectionBase.KeysCollection 執行個體,其中包含 NameObjectCollectionBase 執行個體內的所有索引鍵。Gets a NameObjectCollectionBase.KeysCollection instance that contains all the keys in the NameObjectCollectionBase instance.

(繼承來源 NameObjectCollectionBase)

方法

BaseAdd(String, Object)

將具有指定索引鍵和值的項目加入 NameObjectCollectionBase 執行個體。Adds an entry with the specified key and value into the NameObjectCollectionBase instance.

(繼承來源 NameObjectCollectionBase)
BaseClear()

將所有項目從 NameObjectCollectionBase 執行個體中移除。Removes all entries from the NameObjectCollectionBase instance.

(繼承來源 NameObjectCollectionBase)
BaseGet(Int32)

取得 NameObjectCollectionBase 執行個體指定索引處之項目的值。Gets the value of the entry at the specified index of the NameObjectCollectionBase instance.

(繼承來源 NameObjectCollectionBase)
BaseGet(String)

NameObjectCollectionBase 執行個體取得具有指定索引鍵之第一個項目的值。Gets the value of the first entry with the specified key from the NameObjectCollectionBase instance.

(繼承來源 NameObjectCollectionBase)
BaseGetAllKeys()

傳回 String 陣列,其中包含 NameObjectCollectionBase 執行個體中的所有索引鍵。Returns a String array that contains all the keys in the NameObjectCollectionBase instance.

(繼承來源 NameObjectCollectionBase)
BaseGetAllValues()

傳回 Object 陣列,其中包含 NameObjectCollectionBase 執行個體中的所有值。Returns an Object array that contains all the values in the NameObjectCollectionBase instance.

(繼承來源 NameObjectCollectionBase)
BaseGetAllValues(Type)

傳回指定類型的陣列,其中包含 NameObjectCollectionBase 執行個體中的所有值。Returns an array of the specified type that contains all the values in the NameObjectCollectionBase instance.

(繼承來源 NameObjectCollectionBase)
BaseGetKey(Int32)

取得 NameObjectCollectionBase 執行個體指定索引處之項目的索引鍵。Gets the key of the entry at the specified index of the NameObjectCollectionBase instance.

(繼承來源 NameObjectCollectionBase)
BaseHasKeys()

取得值,表示 NameObjectCollectionBase 執行個體是否包含其索引鍵不是 null 的項目。Gets a value indicating whether the NameObjectCollectionBase instance contains entries whose keys are not null.

(繼承來源 NameObjectCollectionBase)
BaseRemove(String)

將具有指定索引鍵的項目從 NameObjectCollectionBase 中移除。Removes the entries with the specified key from the NameObjectCollectionBase instance.

(繼承來源 NameObjectCollectionBase)
BaseRemoveAt(Int32)

移除 NameObjectCollectionBase 執行個體指定索引處的項目。Removes the entry at the specified index of the NameObjectCollectionBase instance.

(繼承來源 NameObjectCollectionBase)
BaseSet(Int32, Object)

設定 NameObjectCollectionBase 執行個體指定索引處之項目的值。Sets the value of the entry at the specified index of the NameObjectCollectionBase instance.

(繼承來源 NameObjectCollectionBase)
BaseSet(String, Object)

設定 NameObjectCollectionBase 執行個體中具有指定索引鍵之第一個項目的值 (如果有找到),否則將具有指定索引鍵和值的項目加入 NameObjectCollectionBase 執行個體。Sets the value of the first entry with the specified key in the NameObjectCollectionBase instance, if found; otherwise, adds an entry with the specified key and value into the NameObjectCollectionBase instance.

(繼承來源 NameObjectCollectionBase)
CopyTo(Array, Int32)

將檔案集合成員複製到以指定陣列索引起始的 ArrayCopies members of the file collection to an Array beginning at the specified index of the array.

Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(繼承來源 Object)
Get(Int32)

從檔案集合中取得指定之數值索引的 HttpPostedFile 物件。Returns the HttpPostedFile object with the specified numerical index from the file collection.

Get(String)

從檔案集合中傳回指定之名稱的 HttpPostedFile 物件。Returns the HttpPostedFile object with the specified name from the file collection.

GetEnumerator()

傳回在 NameObjectCollectionBase 中逐一查看的列舉值。Returns an enumerator that iterates through the NameObjectCollectionBase.

(繼承來源 NameObjectCollectionBase)
GetHashCode()

做為預設雜湊函式。Serves as the default hash function.

(繼承來源 Object)
GetKey(Int32)

以指定的數值索引傳回 HttpFileCollection 成員的名稱。Returns the name of the HttpFileCollection member with the specified numerical index.

GetMultiple(String)

傳回符合指定名稱的所有檔案。Returns all files that match the specified name.

GetObjectData(SerializationInfo, StreamingContext)

實作 ISerializable 介面,並傳回序列化 NameObjectCollectionBase 執行個體所需的資料。Implements the ISerializable interface and returns the data needed to serialize the NameObjectCollectionBase instance.

(繼承來源 NameObjectCollectionBase)
GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。Creates a shallow copy of the current Object.

(繼承來源 Object)
OnDeserialization(Object)

實作 ISerializable 介面,並於還原序列化完成時引發還原序列化事件。Implements the ISerializable interface and raises the deserialization event when the deserialization is complete.

(繼承來源 NameObjectCollectionBase)
ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(繼承來源 Object)

明確介面實作

ICollection.CopyTo(Array, Int32)

從目標陣列的指定索引開始,將整個 NameObjectCollectionBase 複製到相容的一維 ArrayCopies the entire NameObjectCollectionBase to a compatible one-dimensional Array, starting at the specified index of the target array.

(繼承來源 NameObjectCollectionBase)
ICollection.IsSynchronized

取得值,表示是否要同步處理 (執行緒安全) 對 NameObjectCollectionBase 物件的存取。Gets a value indicating whether access to the NameObjectCollectionBase object is synchronized (thread safe).

(繼承來源 NameObjectCollectionBase)
ICollection.SyncRoot

取得可用來同步處理對 NameObjectCollectionBase 物件之存取的物件。Gets an object that can be used to synchronize access to the NameObjectCollectionBase object.

(繼承來源 NameObjectCollectionBase)

擴充方法

Cast<TResult>(IEnumerable)

IEnumerable 的項目轉換成指定的型別。Casts the elements of an IEnumerable to the specified type.

OfType<TResult>(IEnumerable)

根據指定的型別來篩選 IEnumerable 的項目。Filters the elements of an IEnumerable based on a specified type.

AsParallel(IEnumerable)

啟用查詢的平行化作業。Enables parallelization of a query.

AsQueryable(IEnumerable)

IEnumerable 轉換成 IQueryableConverts an IEnumerable to an IQueryable.

適用於

另請參閱