WebClient.UploadFileAsync Method

Definition

將指定的本機檔案上載至指定的資源。Uploads the specified local file to the specified resource. 這些方法不會封鎖呼叫執行緒。These methods do not block the calling thread.

Overloads

UploadFileAsync(Uri, String)

使用 POST 方法,將指定的本機檔案上載至指定的資源。Uploads the specified local file to the specified resource, using the POST method. 這個方法不會封鎖呼叫執行緒。This method does not block the calling thread.

UploadFileAsync(Uri, String, String)

使用 POST 方法,將指定的本機檔案上載至指定的資源。Uploads the specified local file to the specified resource, using the POST method. 這個方法不會封鎖呼叫執行緒。This method does not block the calling thread.

UploadFileAsync(Uri, String, String, Object)

使用 POST 方法,將指定的本機檔案上載至指定的資源。Uploads the specified local file to the specified resource, using the POST method. 這個方法不會封鎖呼叫執行緒。This method does not block the calling thread.

UploadFileAsync(Uri, String)

使用 POST 方法,將指定的本機檔案上載至指定的資源。Uploads the specified local file to the specified resource, using the POST method. 這個方法不會封鎖呼叫執行緒。This method does not block the calling thread.

public:
 void UploadFileAsync(Uri ^ address, System::String ^ fileName);
public void UploadFileAsync (Uri address, string fileName);
member this.UploadFileAsync : Uri * string -> unit
Public Sub UploadFileAsync (address As Uri, fileName As String)

Parameters

address
Uri

要接收檔案之資源的 URI。The URI of the resource to receive the file. 對於 HTTP 資源而言,這個 URI 必須識別出可以接受使用 POST 方法傳送之要求的資源,例如指令碼或 ASP 網頁。For HTTP resources, this URI must identify a resource that can accept a request sent with the POST method, such as a script or ASP page.

fileName
String

要傳送至資源的檔案。The file to send to the resource.

Exceptions

address 參數是 nullThe address parameter is null. -或--or- fileName 參數是 nullThe fileName parameter is null.

合併 BaseAddressaddress 而形成的 URI 是無效的。The URI formed by combining BaseAddress and address is invalid.

-或--or- fileNamenullEmpty、含有無效的字元,或者指定的檔案路徑不存在。fileName is null, is Empty, contains invalid character, or the specified path to the file does not exist.

-或--or- 開啟資料流時發生錯誤。An error occurred while opening the stream.

-或--or- 沒有來自裝載 (Host) 資源之伺服器的回應。There was no response from the server hosting the resource.

-或--or- Content-type 開頭的 multipart 頁首。The Content-type header begins with multipart.

Remarks

系統會使用從執行緒集區自動設定的執行緒資源,以非同步方式傳送檔案。The file is sent asynchronously using thread resources that are automatically allocated from the thread pool. 若要在檔案上傳完成時收到通知,請將事件處理常式新增至 UploadFileCompleted 事件。To receive notification when the file upload completes, add an event handler to the UploadFileCompleted event.

在傳送檔案時,這個方法不會封鎖呼叫執行緒。This method does not block the calling thread while the file is being sent. 若要在等候伺服器回應時傳送檔案和封鎖,請使用其中一種 UploadFile 方法。To send a file and block while waiting for the server's response, use one of the UploadFile methods.

在 .NET Framework 和 .NET Core 1.0 中,您可以藉由呼叫 CancelAsync 方法來取消尚未完成的非同步作業。In .NET Framework and .NET Core 1.0, you can cancel asynchronous operations that have not completed by calling the CancelAsync method.

如果 BaseAddress 屬性不是空字串(""),則 address 必須是與 BaseAddress 結合的相對 URI,才能形成所要求資料的絕對 URI。If the BaseAddress property is not an empty string (""), address must be a relative URI that is combined with BaseAddress to form the absolute URI of the requested data. 如果 QueryString 屬性不是空字串,則會將它附加至 addressIf the QueryString property is not an empty string, it is appended to address.

這個方法會使用 STOR 命令來上傳 FTP 資源。This method uses the STOR command to upload an FTP resource. 若為 HTTP 資源,則會使用 POST 方法。For an HTTP resource, the POST method is used.

Note

在應用程式中啟用網路追蹤時,這個成員會輸出追蹤資訊。This member outputs trace information when you enable network tracing in your application. 如需詳細資訊,請參閱.NET Framework 中的網路追蹤For more information, see Network Tracing in .NET Framework.

UploadFileAsync(Uri, String, String)

使用 POST 方法,將指定的本機檔案上載至指定的資源。Uploads the specified local file to the specified resource, using the POST method. 這個方法不會封鎖呼叫執行緒。This method does not block the calling thread.

public:
 void UploadFileAsync(Uri ^ address, System::String ^ method, System::String ^ fileName);
public void UploadFileAsync (Uri address, string method, string fileName);
member this.UploadFileAsync : Uri * string * string -> unit
Public Sub UploadFileAsync (address As Uri, method As String, fileName As String)

Parameters

address
Uri

要接收檔案之資源的 URI。The URI of the resource to receive the file. 對於 HTTP 資源而言,這個 URI 必須識別出可以接受使用 POST 方法傳送之要求的資源,例如指令碼或 ASP 網頁。For HTTP resources, this URI must identify a resource that can accept a request sent with the POST method, such as a script or ASP page.

method
String

傳送資料給資源所使用的方法。The method used to send the data to the resource. 如果是 null,預設的 HTTP 為 POST,而 FTP 則為 STOR。If null, the default is POST for http and STOR for ftp.

fileName
String

要傳送至資源的檔案。The file to send to the resource.

Exceptions

address 參數是 nullThe address parameter is null. -或--or- fileName 參數是 nullThe fileName parameter is null.

合併 BaseAddressaddress 而形成的 URI 是無效的。The URI formed by combining BaseAddress and address is invalid.

-或--or- fileNamenullEmpty、含有無效的字元,或者指定的檔案路徑不存在。fileName is null, is Empty, contains invalid character, or the specified path to the file does not exist.

-或--or- 開啟資料流時發生錯誤。An error occurred while opening the stream.

-或--or- 沒有來自裝載 (Host) 資源之伺服器的回應。There was no response from the server hosting the resource.

-或--or- Content-type 開頭的 multipart 頁首。The Content-type header begins with multipart.

Remarks

系統會使用從執行緒集區自動設定的執行緒資源,以非同步方式傳送檔案。The file is sent asynchronously using thread resources that are automatically allocated from the thread pool. 若要在檔案上傳完成時收到通知,請將事件處理常式新增至 UploadFileCompleted 事件。To receive notification when the file upload completes, add an event handler to the UploadFileCompleted event.

在傳送檔案時,這個方法不會封鎖呼叫執行緒。This method does not block the calling thread while the file is being sent. 若要在等候伺服器回應時傳送檔案和封鎖,請使用其中一種 UploadFile 方法。To send a file and block while waiting for the server's response, use one of the UploadFile methods.

在 .NET Framework 和 .NET Core 1.0 中,您可以藉由呼叫 CancelAsync 方法來取消尚未完成的非同步作業。In .NET Framework and .NET Core 1.0, you can cancel asynchronous operations that have not completed by calling the CancelAsync method.

如果 BaseAddress 屬性不是空字串(""),則 address 必須是與 BaseAddress 結合的相對 URI,才能形成所要求資料的絕對 URI。If the BaseAddress property is not an empty string (""), address must be a relative URI that is combined with BaseAddress to form the absolute URI of the requested data. 如果 QueryString 屬性不是空字串,則會將它附加至 addressIf the QueryString property is not an empty string, it is appended to address.

這個方法會使用 STOR 命令來上傳 FTP 資源。This method uses the STOR command to upload an FTP resource. 若為 HTTP 資源,則會使用 POST 方法。For an HTTP resource, the POST method is used.

Note

在應用程式中啟用網路追蹤時,這個成員會輸出追蹤資訊。This member outputs trace information when you enable network tracing in your application. 如需詳細資訊,請參閱.NET Framework 中的網路追蹤For more information, see Network Tracing in .NET Framework.

UploadFileAsync(Uri, String, String, Object)

使用 POST 方法,將指定的本機檔案上載至指定的資源。Uploads the specified local file to the specified resource, using the POST method. 這個方法不會封鎖呼叫執行緒。This method does not block the calling thread.

public:
 void UploadFileAsync(Uri ^ address, System::String ^ method, System::String ^ fileName, System::Object ^ userToken);
public void UploadFileAsync (Uri address, string method, string fileName, object userToken);
member this.UploadFileAsync : Uri * string * string * obj -> unit
Public Sub UploadFileAsync (address As Uri, method As String, fileName As String, userToken As Object)

Parameters

address
Uri

要接收檔案之資源的 URI。The URI of the resource to receive the file. 對於 HTTP 資源而言,這個 URI 必須識別出可以接受使用 POST 方法傳送之要求的資源,例如指令碼或 ASP 網頁。For HTTP resources, this URI must identify a resource that can accept a request sent with the POST method, such as a script or ASP page.

method
String

傳送資料給資源所使用的方法。The method used to send the data to the resource. 如果是 null,預設的 HTTP 為 POST,而 FTP 則為 STOR。If null, the default is POST for http and STOR for ftp.

fileName
String

要傳送至資源的檔案。The file to send to the resource.

userToken
Object

使用者定義的物件,這個物件會在非同步作業完成時傳遞至叫用的方法。A user-defined object that is passed to the method invoked when the asynchronous operation completes.

Exceptions

address 參數是 nullThe address parameter is null. -或--or- fileName 參數是 nullThe fileName parameter is null.

合併 BaseAddressaddress 而形成的 URI 是無效的。The URI formed by combining BaseAddress and address is invalid.

-或--or- fileNamenullEmpty、含有無效的字元,或者指定的檔案路徑不存在。fileName is null, is Empty, contains invalid character, or the specified path to the file does not exist.

-或--or- 開啟資料流時發生錯誤。An error occurred while opening the stream.

-或--or- 沒有來自裝載 (Host) 資源之伺服器的回應。There was no response from the server hosting the resource.

-或--or- Content-type 開頭的 multipart 頁首。The Content-type header begins with multipart.

Remarks

系統會使用從執行緒集區自動設定的執行緒資源,以非同步方式傳送檔案。The file is sent asynchronously using thread resources that are automatically allocated from the thread pool. 若要在檔案上傳完成時收到通知,請將事件處理常式新增至 UploadFileCompleted 事件。To receive notification when the file upload completes, add an event handler to the UploadFileCompleted event.

在傳送檔案時,這個方法不會封鎖呼叫執行緒。This method does not block the calling thread while the file is being sent. 若要在等候伺服器回應時傳送檔案和封鎖,請使用其中一種 UploadFile 方法。To send a file and block while waiting for the server's response, use one of the UploadFile methods.

在 .NET Framework 和 .NET Core 1.0 中,您可以藉由呼叫 CancelAsync 方法來取消尚未完成的非同步作業。In .NET Framework and .NET Core 1.0, you can cancel asynchronous operations that have not completed by calling the CancelAsync method.

如果 BaseAddress 屬性不是空字串(""),則 address 必須是與 BaseAddress 結合的相對 URI,才能形成所要求資料的絕對 URI。If the BaseAddress property is not an empty string (""), address must be a relative URI that is combined with BaseAddress to form the absolute URI of the requested data. 如果 QueryString 屬性不是空字串,則會將它附加至 addressIf the QueryString property is not an empty string, it is appended to address.

這個方法會使用 STOR 命令來上傳 FTP 資源。This method uses the STOR command to upload an FTP resource. 若為 HTTP 資源,則會使用 POST 方法。For an HTTP resource, the POST method is used.

Note

在應用程式中啟用網路追蹤時,這個成員會輸出追蹤資訊。This member outputs trace information when you enable network tracing in your application. 如需詳細資訊,請參閱.NET Framework 中的網路追蹤For more information, see Network Tracing in .NET Framework.

Applies to