แก้ไข

แชร์ผ่าน


WebClient.DownloadFileAsync Method

Definition

Downloads the specified resource to a local file as an asynchronous operation. These methods do not block the calling thread.

Overloads

DownloadFileAsync(Uri, String)

Downloads, to a local file, the resource with the specified URI. This method does not block the calling thread.

DownloadFileAsync(Uri, String, Object)

Downloads, to a local file, the resource with the specified URI. This method does not block the calling thread.

DownloadFileAsync(Uri, String)

Source:
WebClient.cs
Source:
WebClient.cs
Source:
WebClient.cs

Downloads, to a local file, the resource with the specified URI. This method does not block the calling thread.

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

Parameters

address
Uri

The URI of the resource to download.

fileName
String

The name of the file to be placed on the local computer.

Exceptions

The address parameter is null.

-or-

The fileName parameter is null.

The URI formed by combining BaseAddress and address is invalid.

-or-

An error occurred while downloading the resource.

The local file specified by fileName is in use by another thread.

Remarks

Caution

WebRequest, HttpWebRequest, ServicePoint, and WebClient are obsolete, and you shouldn't use them for new development. Use HttpClient instead.

This method downloads the resource at the URI specified by in the address parameter. When the download completes successfully, the downloaded file is named fileName on the local computer. The file is downloaded asynchronously using thread resources that are automatically allocated from the thread pool. To receive notification when the file is available, add an event handler to the DownloadFileCompleted event.

This method does not block the calling thread while the resource is being downloaded. To block while waiting for the download to complete, use one of the DownloadFile methods.

In .NET Framework and .NET Core 1.0, you can cancel asynchronous operations that have not completed by calling the CancelAsync method.

If the BaseAddress property is not an empty string ("") and address does not specify an absolute URI, address must be a relative URI that is combined with BaseAddress to form the absolute URI of the requested data. If the QueryString property is not an empty string, it is appended to address.

This method uses the RETR command to download an FTP resource. For an HTTP resource, the GET method is used.

Note

This member outputs trace information when you enable network tracing in your application. For more information, see Network Tracing in .NET Framework.

When using this method in an ASP.NET page, you will receive an error if the account that the page executes under does not have permission to access the local file.

This method stores in the task it returns all non-usage exceptions that the method's synchronous counterpart can throw. If an exception is stored into the returned task, that exception will be thrown when the task is awaited. Usage exceptions, such as ArgumentException, are still thrown synchronously. For the stored exceptions, see the exceptions thrown by DownloadFile(Uri, String).

Applies to

DownloadFileAsync(Uri, String, Object)

Source:
WebClient.cs
Source:
WebClient.cs
Source:
WebClient.cs

Downloads, to a local file, the resource with the specified URI. This method does not block the calling thread.

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

Parameters

address
Uri

The URI of the resource to download.

fileName
String

The name of the file to be placed on the local computer.

userToken
Object

A user-defined object that is passed to the method invoked when the asynchronous operation completes.

Exceptions

The address parameter is null.

-or-

The fileName parameter is null.

The URI formed by combining BaseAddress and address is invalid.

-or-

An error occurred while downloading the resource.

The local file specified by fileName is in use by another thread.

Remarks

Caution

WebRequest, HttpWebRequest, ServicePoint, and WebClient are obsolete, and you shouldn't use them for new development. Use HttpClient instead.

This method downloads the resource at the URI specified by in the address parameter. When the download completes successfully, the downloaded file is named fileName on the local computer. The file is downloaded asynchronously using thread resources that are automatically allocated from the thread pool. To receive notification when the file is available, add an event handler to the DownloadFileCompleted event.

This method does not block the calling thread while the resource is being downloaded. To block while waiting for the download to complete, use one of the DownloadFile methods.

In .NET Framework and .NET Core 1.0, you can cancel asynchronous operations that have not completed by calling the CancelAsync method.

If the BaseAddress property is not an empty string ("") and address does not specify an absolute URI, address must be a relative URI that is combined with BaseAddress to form the absolute URI of the requested data. If the QueryString property is not an empty string, it is appended to address.

This method uses the RETR command to download an FTP resource. For an HTTP resource, the GET method is used.

Note

This member outputs trace information when you enable network tracing in your application. For more information, see Network Tracing in .NET Framework.

When using this method in an ASP.NET page, you will receive an error if the account that the page executes under does not have permission to access the local file.

Applies to