次の方法で共有


WebClient.UploadFile メソッド (String, String)

指定した URI を持つリソースへ指定したローカル ファイルをアップロードします。

Overloads Public Function UploadFile( _
   ByVal address As String, _   ByVal fileName As String _) As Byte()
[C#]
public byte[] UploadFile(stringaddress,stringfileName);
[C++]
public: unsigned char UploadFile(String* address,String* fileName)  __gc[];
[JScript]
public function UploadFile(
   address : String,fileName : String) : Byte[];

パラメータ

  • address
    ファイルを受信するリソースの URI。この URI は、POST 要求を受け入れることができるリソース (スクリプトや ASP ページなど) を識別するものである必要があります。
  • fileName
    リソースに送信するファイル。

戻り値

リソースからの応答の本体を保持しているバイトの配列。

例外

例外の種類 条件
WebException BaseAddress および address を組み合わせて形成された URI が無効です。

または

fileName が null 参照 (Visual Basic では Nothing) または Empty であるか、無効な文字を含んでいます。あるいは、指定したファイルへのパスが見つかりません。

または

ストリームのオープン中にエラーが発生しました。

または

リソースをホストしているサーバーから応答がありませんでした。

または

Content-type ヘッダーは "multipart" で始まります。

SecurityException ローカル ファイルへのアクセス権が与えられていません。

解説

UploadFile メソッドは、リソースにローカル ファイルを送信します。基になる要求は、メソッド動詞 POST を使用して作成されています。

動詞 POST は、HTTP によって定義されます。基になる要求が HTTP を使用せず、POST がサーバーまたは address リソースによって認識されない場合は、発生する内容は、基になるプロトコル クラスによって決定されます。通常は、エラーを示す Status プロパティ セットと共に WebException がスローされます。

BaseAddress プロパティが空でない場合、 address は、相対 URI である必要があります。この相対 URI は、 BaseAddress と結合されて、要求されたデータの絶対 URI を形成します。 QueryString プロパティが空でない場合は、 address に追加されます。

使用例

[Visual Basic, C#, C++] UploadFile を使用して、指定したファイルを指定した URI にアップロードする例を次に示します。サーバーが返した応答がコンソールに表示されます。

 
Console.Write(ControlChars.Cr + "Please enter the URI to post data to : ")
Dim uriString As String = Console.ReadLine()

' Create a new WebClient instance.
Dim myWebClient As New WebClient()

Console.WriteLine(ControlChars.Cr + "Please enter the fully qualified path of the file to be uploaded to the URI")
Dim fileName As String = Console.ReadLine()
Console.WriteLine("Uploading {0} to {1} ...", fileName, uriString)

' Upload the file to the URI.
' The 'UploadFile(uriString,fileName)' method implicitly uses HTTP POST method. 
Dim responseArray As Byte() = myWebClient.UploadFile(uriString, fileName)

' Decode and display the response.
Console.WriteLine(ControlChars.Cr + "Response Received.The contents of the file uploaded are: " + ControlChars.Cr + "{0}", Encoding.ASCII.GetString(responseArray))

[C#] 
Console.Write("\nPlease enter the URI to post data to : ");
String uriString = Console.ReadLine();

// Create a new WebClient instance.
WebClient myWebClient = new WebClient();

Console.WriteLine("\nPlease enter the fully qualified path of the file to be uploaded to the URI");
string fileName = Console.ReadLine();
Console.WriteLine("Uploading {0} to {1} ...",fileName,uriString);                        
// Upload the file to the URI.
         // The 'UploadFile(uriString,fileName)' method implicitly uses HTTP POST method.
byte[] responseArray = myWebClient.UploadFile(uriString,fileName);

// Decode and display the response.
Console.WriteLine("\nResponse Received.The contents of the file uploaded are: \n{0}",Encoding.ASCII.GetString(responseArray));

[C++] 
Console::Write(S"\nPlease enter the URI to post data to : ");
String*  uriString = Console::ReadLine();

// Create a new WebClient instance.
WebClient* myWebClient = new WebClient();

Console::WriteLine(S"\nPlease enter the fully qualified path of the file to be uploaded to the URI");
String* fileName = Console::ReadLine();
Console::WriteLine(S"Uploading {0} to {1} ...", fileName, uriString);
// Upload the file to the URI.
// The 'UploadFile(uriString, fileName)' method implicitly uses HTTP POST method.
Byte responseArray[] = myWebClient->UploadFile(uriString, fileName);

// Decode and display the response.
Console::WriteLine(S"\nResponse Received::The contents of the file uploaded are: \n {0}", Encoding::ASCII->GetString(responseArray));

[Visual Basic, C#, C++] ASP.NET ページの例を次に示します。この ASP.NET ページは、ポストされたファイルの受け入れが可能で、また UploadFile メソッドの使用にも適しています。このページは Web サーバー上に常駐させる必要があります。そのアドレスにより、 UploadFile メソッドの address パラメータの値が提供されます。

 
<%@ Import Namespace="System"%>
<%@ Import Namespace="System.IO"%>
<%@ Import Namespace="System.Net"%>
<%@ Import NameSpace="System.Web"%>

<Script language="VB" runat=server>
    Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
        
        Dim f As String
        Dim file
        For Each f In Request.Files.AllKeys
            file = Request.Files(f)
            file.SaveAs("c:\inetpub\test\UploadedFiles\" & file.FileName)
        Next f
        
    End Sub

</Script>
<html>
<body>
<p> Upload complete. </p>
</body>
</html>

[C#] 
<%@ Import Namespace="System"%>
<%@ Import Namespace="System.IO"%>
<%@ Import Namespace="System.Net"%>
<%@ Import NameSpace="System.Web"%>

<Script language="C#" runat=server>
void Page_Load(object sender, EventArgs e) {
    
    foreach(string f in Request.Files.AllKeys) {
        HttpPostedFile file = Request.Files[f];
        file.SaveAs("c:\\inetpub\\test\\UploadedFiles\\" + file.FileName);
    }    
}

</Script>
<html>
<body>
<p> Upload complete.  </p>
</body>
</html>

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, Common Language Infrastructure (CLI) Standard

参照

WebClient クラス | WebClient メンバ | System.Net 名前空間 | WebClient.UploadFile オーバーロードの一覧