WebClient.UploadFile 方法

定義

將本機檔案上載至具有指定之 URI 的資源。

多載

UploadFile(String, String)

將指定的本機檔案上載至具有指定之 URI 的資源。

UploadFile(Uri, String)

將指定的本機檔案上載至具有指定之 URI 的資源。

UploadFile(String, String, String)

使用指定的方法,將指定的本機檔案上載至指定的資源。

UploadFile(Uri, String, String)

使用指定的方法,將指定的本機檔案上載至指定的資源。

UploadFile(String, String)

將指定的本機檔案上載至具有指定之 URI 的資源。

public:
 cli::array <System::Byte> ^ UploadFile(System::String ^ address, System::String ^ fileName);
public byte[] UploadFile (string address, string fileName);
member this.UploadFile : string * string -> byte[]
Public Function UploadFile (address As String, fileName As String) As Byte()

參數

address
String

要接收檔案之資源的 URI。 例如 ftp://localhost/samplefile.txt.

fileName
String

要傳送至資源的檔案。 例如,"samplefile.txt"。

傳回

Byte[]

Byte 陣列,包含來自資源的回應主體。

例外狀況

address 參數為 null

-或-

fileName 參數為 null

合併 BaseAddressaddress 而形成的 URI 是無效的。

-或-

fileNamenullEmpty,表示其中包含無效的字元,或其不存在。

-或-

上載檔案時發生錯誤。

-或-

沒有來自裝載 (Host) 資源之伺服器的回應。

-或-

Content-type 開頭的 multipart 頁首。

範例

下列程式碼範例會使用 UploadFile 將指定的檔案上傳至指定的 URI。 伺服器傳回的任何回應都會顯示在主控台上。

Console::Write( "\nPlease enter the URI to post data to : " );
String^ uriString = Console::ReadLine();

// Create a new WebClient instance.
WebClient^ myWebClient = gcnew 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.
array<Byte>^responseArray = myWebClient->UploadFile( uriString, fileName );

// Decode and display the response.
Console::WriteLine( "\nResponse Received::The contents of the file uploaded are: \n {0}", 
    System::Text::Encoding::ASCII->GetString( responseArray ) );
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}", 
    System.Text.Encoding.ASCII.GetString(responseArray));

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}", System.Text.Encoding.ASCII.GetString(responseArray))

下列程式碼範例顯示可接受張貼檔案且適合與 方法搭配 UploadFile 使用的 ASP.NET 網頁。 頁面必須位於網頁伺服器上。 其位址會提供 方法之 UploadFile 參數的值 address

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

備註

方法會將 UploadFile 本機檔案傳送至資源。 此方法會使用 STOR 命令來上傳 FTP 資源。 針對 HTTP 資源,會使用 POST 方法。

這個方法會在上傳檔案時封鎖。 若要在等候伺服器的回應時繼續執行,請使用其中 UploadFileAsync 一種方法。

方法 POST 是由 HTTP 所定義。 如果基礎要求不使用 HTTP,而且 POST 伺服器無法瞭解,基礎通訊協定類別會決定會發生什麼情況。 一般而言, WebException 會擲回 Status 屬性集來表示錯誤。

BaseAddress如果屬性不是空字串 (「」) 且 address 不包含絕對 URI, address 則必須是結合 BaseAddress 的相對 URI,才能形成所要求資料的絕對 URI。 QueryString如果屬性不是空字串,則會附加至 address

注意

在應用程式中啟用網路追蹤時,這個成員會輸出追蹤資訊。 如需詳細資訊,請參閱.NET Framework 中的網路追蹤

適用於

UploadFile(Uri, String)

將指定的本機檔案上載至具有指定之 URI 的資源。

public:
 cli::array <System::Byte> ^ UploadFile(Uri ^ address, System::String ^ fileName);
public byte[] UploadFile (Uri address, string fileName);
member this.UploadFile : Uri * string -> byte[]
Public Function UploadFile (address As Uri, fileName As String) As Byte()

參數

address
Uri

要接收檔案之資源的 URI。 例如 ftp://localhost/samplefile.txt.

fileName
String

要傳送至資源的檔案。 例如,"samplefile.txt"。

傳回

Byte[]

Byte 陣列,包含來自資源的回應主體。

例外狀況

address 參數為 null

-或-

fileName 參數為 null

合併 BaseAddressaddress 而形成的 URI 是無效的。

-或-

fileNamenullEmpty,表示其中包含無效的字元,或其不存在。

-或-

上載檔案時發生錯誤。

-或-

沒有來自裝載 (Host) 資源之伺服器的回應。

-或-

Content-type 開頭的 multipart 頁首。

備註

方法會將 UploadFile 本機檔案傳送至資源。 此方法會使用 STOR 命令來上傳 FTP 資源。 針對 HTTP 資源,會使用 POST 方法。

這個方法會在上傳檔案時封鎖。 若要在等候伺服器的回應時繼續執行,請使用其中 UploadFileAsync 一種方法。

方法 POST 是由 HTTP 所定義。 如果基礎要求不使用 HTTP,而且 POST 伺服器無法瞭解,基礎通訊協定類別會決定會發生什麼情況。 一般而言, WebException 會擲回 Status 屬性集來表示錯誤。

BaseAddress如果屬性不是空字串 (「」) 且 address 不包含絕對 URI, address 則必須是結合 BaseAddress 的相對 URI,才能形成所要求資料的絕對 URI。 QueryString如果屬性不是空字串,則會附加至 address

注意

在應用程式中啟用網路追蹤時,這個成員會輸出追蹤資訊。 如需詳細資訊,請參閱.NET Framework 中的網路追蹤

適用於

UploadFile(String, String, String)

使用指定的方法,將指定的本機檔案上載至指定的資源。

public:
 cli::array <System::Byte> ^ UploadFile(System::String ^ address, System::String ^ method, System::String ^ fileName);
public byte[] UploadFile (string address, string? method, string fileName);
public byte[] UploadFile (string address, string method, string fileName);
member this.UploadFile : string * string * string -> byte[]
Public Function UploadFile (address As String, method As String, fileName As String) As Byte()

參數

address
String

要接收檔案之資源的 URI。

method
String

傳送檔案給資源所使用的方法。 如果是 null,預設的 HTTP 為 POST,而 FTP 則為 STOR。

fileName
String

要傳送至資源的檔案。

傳回

Byte[]

Byte 陣列,包含來自資源的回應主體。

例外狀況

address 參數為 null

-或-

fileName 參數為 null

合併 BaseAddressaddress 而形成的 URI 是無效的。

-或-

fileNamenullEmpty,表示其中包含無效的字元,或其不存在。

-或-

上載檔案時發生錯誤。

-或-

沒有來自裝載 (Host) 資源之伺服器的回應。

-或-

Content-type 開頭的 multipart 頁首。

範例

下列程式碼範例會使用 UploadFile 將指定的檔案上傳至指定的 URI。 伺服器傳回的任何回應都會顯示在主控台上。

Console::Write( "\nPlease enter the URL to post data to : " );
String^ uriString = Console::ReadLine();

// Create a new WebClient instance.
WebClient^ myWebClient = gcnew 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.
array<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}", 
    System::Text::Encoding::ASCII->GetString( responseArray ));
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}",
    System.Text.Encoding.ASCII.GetString(responseArray));


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}", System.Text.Encoding.ASCII.GetString(responseArray))

下列程式碼範例顯示可接受張貼檔案且適合與 方法搭配 UploadFile 使用的 ASP.NET 網頁。 頁面必須位於網頁伺服器上。 其位址會提供 方法之 UploadFile 參數的值 address

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

備註

當 address 指定 HTTP 資源時,方法會 UploadFile 使用 參數中指定的 method HTTP 方法,將本機檔案傳送至資源,並從伺服器傳回任何回應。 這個方法會在上傳檔案時封鎖。 若要在等候伺服器的回應時繼續執行,請使用其中 UploadFileAsync 一種方法。

method如果 參數指定伺服器或 address 資源無法理解的動詞命令,基礎通訊協定類別會決定會發生什麼情況。 一般而言, WebException 會擲回 Status 屬性集來表示錯誤。

BaseAddress如果屬性不是空字串 (「」) 且 address 不包含絕對 URI, address 則必須是結合 BaseAddress 的相對 URI,才能形成所要求資料的絕對 URI。 QueryString如果屬性不是空字串,則會附加至 address

注意

在應用程式中啟用網路追蹤時,這個成員會輸出追蹤資訊。 如需詳細資訊,請參閱.NET Framework 中的網路追蹤

適用於

UploadFile(Uri, String, String)

使用指定的方法,將指定的本機檔案上載至指定的資源。

public:
 cli::array <System::Byte> ^ UploadFile(Uri ^ address, System::String ^ method, System::String ^ fileName);
public byte[] UploadFile (Uri address, string? method, string fileName);
public byte[] UploadFile (Uri address, string method, string fileName);
member this.UploadFile : Uri * string * string -> byte[]
Public Function UploadFile (address As Uri, method As String, fileName As String) As Byte()

參數

address
Uri

要接收檔案之資源的 URI。

method
String

傳送檔案給資源所使用的方法。 如果是 null,預設的 HTTP 為 POST,而 FTP 則為 STOR。

fileName
String

要傳送至資源的檔案。

傳回

Byte[]

Byte 陣列,包含來自資源的回應主體。

例外狀況

address 參數為 null

-或-

fileName 參數為 null

合併 BaseAddressaddress 而形成的 URI 是無效的。

-或-

fileNamenullEmpty,表示其中包含無效的字元,或其不存在。

-或-

上載檔案時發生錯誤。

-或-

沒有來自裝載 (Host) 資源之伺服器的回應。

-或-

Content-type 開頭的 multipart 頁首。

備註

當 address 指定 HTTP 資源時,方法會 UploadFile 使用 參數中指定的 method HTTP 方法,將本機檔案傳送至資源,並從伺服器傳回任何回應。 這個方法會在上傳檔案時封鎖。 若要在等候伺服器的回應時繼續執行,請使用其中 UploadFileAsync 一種方法。

method如果 參數指定伺服器或 address 資源無法理解的動詞命令,基礎通訊協定類別會決定會發生什麼情況。 一般而言, WebException 會擲回 Status 屬性集來表示錯誤。

BaseAddress如果屬性不是空字串 (「」) 且 address 不包含絕對 URI, address 則必須是結合 BaseAddress 的相對 URI,才能形成所要求資料的絕對 URI。 QueryString如果屬性不是空字串,則會附加至 address

注意

在應用程式中啟用網路追蹤時,這個成員會輸出追蹤資訊。 如需詳細資訊,請參閱.NET Framework 中的網路追蹤

適用於