WebClient.UploadFile メソッド
指定した URI を持つリソースへローカル ファイルをアップロードします。
オーバーロードの一覧
指定した URI を持つリソースへ指定したローカル ファイルをアップロードします。
[Visual Basic] Overloads Public Function UploadFile(String, String) As Byte()
[C++] public: unsigned char UploadFile(String*, String*) __gc[];
[JScript] public function UploadFile(String, String) : Byte[];
指定したメソッドを使用して、指定したリソースへ指定したローカル ファイルをアップロードします。
[Visual Basic] Overloads Public Function UploadFile(String, String, String) As Byte()
[C++] public: unsigned char UploadFile(String*, String*, String*) __gc[];
[JScript] public function UploadFile(String, String, String) : Byte[];
使用例
[Visual Basic, C#, C++] UploadFile を使用して、指定したファイルを指定した URI にアップロードする例を次に示します。サーバーが返した応答がコンソールに表示されます。
[Visual Basic, C#, C++] メモ ここでは、UploadFile のオーバーロード形式のうちの 1 つだけについて、使用例を示します。その他の例については、各オーバーロード形式のトピックを参照してください。
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++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン をクリックします。