次の方法で共有


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

指定したメソッドを使用して、指定したリソースへ指定したローカル ファイルをアップロードします。

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

パラメータ

  • address
    ファイルを受信するリソースの URI。この URI は、 method パラメータで指定されたメソッドが送信する要求を受け入れることができるリソース (スクリプトや ASP ページなど) を識別するものである必要があります。
  • method
    リソースにファイルを送信するために使用するメソッド動詞。
  • fileName
    リソースに送信するファイル。

戻り値

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

例外

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

または

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

または

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

または

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

または

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

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

解説

UploadFile メソッドは、 method パラメータで指定したメソッド動詞を使用してリソースにローカル ファイルを送信し、サーバーからの応答を返します。

method パラメータによってサーバーが認識できない動詞が指定された場合は、発生する内容は、基になるプロトコル クラスによって決定されます。通常は、エラーを示す Status プロパティ セットと共に WebException がスローされます。

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

使用例

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

 
Console.Write(ControlChars.Cr + "Please enter the URL 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 URL")

Dim fileName As String = Console.ReadLine()
Console.WriteLine("Uploading {0} to {1} ...", fileName, uriString)

' Upload the file to the Url using the HTTP 1.0 POST.
Dim responseArray As Byte() = myWebClient.UploadFile(uriString, "POST", 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 URL 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 URL");
string fileName = Console.ReadLine();

Console.WriteLine("Uploading {0} to {1} ...",fileName,uriString);                        
// Upload the file to the URL using the HTTP 1.0 POST.
byte[] responseArray = myWebClient.UploadFile(uriString,"POST",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 URL 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 URL");
String* fileName = Console::ReadLine();

Console::WriteLine(S"Uploading {0} to {1} ...", fileName, uriString);
// Upload the file to the URL using the HTTP 1.0 POST.
Byte responseArray[] = myWebClient->UploadFile(uriString, S"POST", 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 オーバーロードの一覧