Share via


HttpFileCollection クラス

クライアントによってアップロードされたファイルへのアクセスを実現し、そのファイルを編成します。

名前空間: System.Web
アセンブリ: System.Web (system.web.dll 内)

構文

'宣言
Public NotInheritable Class HttpFileCollection
    Inherits NameObjectCollectionBase
'使用
Dim instance As HttpFileCollection
public sealed class HttpFileCollection : NameObjectCollectionBase
public ref class HttpFileCollection sealed : public NameObjectCollectionBase
public final class HttpFileCollection extends NameObjectCollectionBase
public final class HttpFileCollection extends NameObjectCollectionBase
適用できません。

解説

クライアントは、ファイルをエンコードし、そのファイルをマルチパート MIME 形式を使用して multipart/form-data の HTTP Content-Type ヘッダーと共にコンテンツ本体に送信します。ASP.NET は、エンコードされたファイルを、コンテンツ本体から HttpFileCollection の個別のメンバへ抽出します。HttpPostedFile クラスのメソッドとプロパティを使用して、各ファイルのコンテンツとプロパティにアクセスできます。

使用例

HttpRequest オブジェクトの Files プロパティから返された HttpFileCollection コレクションにアクセスする方法を次の例に示します。HttpFileCollection コレクションは、Web ページ上の 2 つの FileUpload コントロールによって設定されます。ファイル コレクション内の項目は、BulletedList コントロールに表示されます。

<%@ 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>
<%@ 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>

.NET Framework のセキュリティ

継承階層

System.Object
   System.Collections.Specialized.NameObjectCollectionBase
    System.Web.HttpFileCollection

スレッド セーフ

この型の public static (Visual Basicでは共有) メンバはすべて,スレッド セーフです。インスタンス メンバの場合は,スレッド セーフであるとは限りません。

プラットフォーム

Windows 98,Windows Server 2000 SP4,Windows CE,Windows Millennium Edition,Windows Mobile for Pocket PC,Windows Mobile for Smartphone,Windows Server 2003,Windows XP Media Center Edition,Windows XP Professional x64 Edition,Windows XP SP2,Windows XP Starter Edition

Microsoft .NET Framework 3.0 は Windows Vista,Microsoft Windows XP SP2,および Windows Server 2003 SP1 でサポートされています。

バージョン情報

.NET Framework

サポート対象 : 3.0,2.0,1.1,1.0

参照

関連項目

HttpFileCollection メンバ
System.Web 名前空間
HttpPostedFile
HtmlInputFile