WebClient.DownloadFile 方法
定义
将具有指定 URI 的资源下载到本地文件。Downloads the resource with the specified URI to a local file.
重载
| DownloadFile(Uri, String) |
将具有指定 URI 的资源下载到本地文件。Downloads the resource with the specified URI to a local file. |
| DownloadFile(String, String) |
将具有指定 URI 的资源下载到本地文件。Downloads the resource with the specified URI to a local file. |
DownloadFile(Uri, String)
将具有指定 URI 的资源下载到本地文件。Downloads the resource with the specified URI to a local file.
public:
void DownloadFile(Uri ^ address, System::String ^ fileName);
public void DownloadFile (Uri address, string fileName);
member this.DownloadFile : Uri * string -> unit
Public Sub DownloadFile (address As Uri, fileName As String)
参数
- fileName
- String
要接收数据的本地文件的名称。The name of the local file that is to receive the data.
例外
address 参数为 null。The address parameter is null.
- 或 --or-
fileName 参数为 null。The fileName parameter is null.
通过组合 BaseAddress 和 address 所构成的 URI 无效。The URI formed by combining BaseAddress and address is invalid.
- 或 --or-
filename 是 null 或 Empty。filename is null or Empty.
- 或 --or-
文件不存在。The file does not exist.
- 或 --or-
下载数据时发生错误。An error occurred while downloading data.
该方法已在多个线程上同时调用。The method has been called simultaneously on multiple threads.
注解
DownloadFile方法从参数中指定的 URI 下载到本地文件数据 address 。The DownloadFile method downloads to a local file data from the URI specified by in the address parameter. 此方法在下载资源时阻止。This method blocks while downloading the resource. 若要下载资源并在等待服务器响应时继续执行,请使用其中一种 DownloadFileAsync 方法。To download a resource and continue executing while waiting for the server's response, use one of the DownloadFileAsync methods.
如果该 BaseAddress 属性不是空字符串 ( "" ) 并且不 address 包含绝对 uri, address 则必须是与组合的相对 uri, BaseAddress 才能形成所请求数据的绝对 uri。If the BaseAddress property is not an empty string ("") and address does not contain an absolute URI, address must be a relative URI that is combined with BaseAddress to form the absolute URI of the requested data. 如果该 QueryString 属性不是空字符串,则将其追加到 address 。If the QueryString property is not an empty string, it is appended to address.
此方法使用 RETR 命令下载 FTP 资源。This method uses the RETR command to download an FTP resource. 对于 HTTP 资源,使用 GET 方法。For an HTTP resource, the GET method is used.
备注
当你在应用程序中启用网络跟踪后,此成员将输出跟踪信息。This member outputs trace information when you enable network tracing in your application. 有关详细信息,请参阅 .NET Framework 中的网络跟踪。For more information, see Network Tracing in .NET Framework.
在中间层应用程序中使用此方法时,例如 ASP.NET 页,如果执行应用程序的帐户没有访问该文件的权限,则会收到错误。When using this method in a middle tier application, such as an ASP.NET page, you will receive an error if the account under which the application executes does not have permission to access the file.
适用于
DownloadFile(String, String)
将具有指定 URI 的资源下载到本地文件。Downloads the resource with the specified URI to a local file.
public:
void DownloadFile(System::String ^ address, System::String ^ fileName);
public void DownloadFile (string address, string fileName);
member this.DownloadFile : string * string -> unit
Public Sub DownloadFile (address As String, fileName As String)
参数
- address
- String
从中下载数据的 URI。The URI from which to download data.
- fileName
- String
要接收数据的本地文件的名称。The name of the local file that is to receive the data.
例外
address 参数为 null。The address parameter is null.
通过组合 BaseAddress 和 address 所构成的 URI 无效。The URI formed by combining BaseAddress and address is invalid.
- 或 --or-
filename 是 null 或 Empty。filename is null or Empty.
- 或 --or-
文件不存在。The file does not exist.
- 或 - 下载数据时发生错误。-or- An error occurred while downloading data.
该方法已在多个线程上同时调用。The method has been called simultaneously on multiple threads.
示例
下面的代码示例将文件从下载 http://www.contoso.com 到本地硬盘驱动器。The following code example downloads a file from http://www.contoso.com to the local hard drive.
String^ remoteUri = "http://www.contoso.com/library/homepage/images/";
String^ fileName = "ms-banner.gif", ^myStringWebResource = nullptr;
// Create a new WebClient instance.
WebClient^ myWebClient = gcnew WebClient;
// Concatenate the domain with the Web resource filename.
myStringWebResource = String::Concat( remoteUri, fileName );
Console::WriteLine( "Downloading File \"{0}\" from \"{1}\" .......\n\n", fileName, myStringWebResource );
// Download the Web resource and save it into the current filesystem folder.
myWebClient->DownloadFile( myStringWebResource, fileName );
Console::WriteLine( "Successfully Downloaded File \"{0}\" from \"{1}\"", fileName, myStringWebResource );
Console::WriteLine( "\nDownloaded file saved in the following file system folder:\n\t {0}", Application::StartupPath );
string remoteUri = "http://www.contoso.com/library/homepage/images/";
string fileName = "ms-banner.gif", myStringWebResource = null;
// Create a new WebClient instance.
WebClient myWebClient = new WebClient();
// Concatenate the domain with the Web resource filename.
myStringWebResource = remoteUri + fileName;
Console.WriteLine("Downloading File \"{0}\" from \"{1}\" .......\n\n", fileName, myStringWebResource);
// Download the Web resource and save it into the current filesystem folder.
myWebClient.DownloadFile(myStringWebResource,fileName);
Console.WriteLine("Successfully Downloaded File \"{0}\" from \"{1}\"", fileName, myStringWebResource);
Console.WriteLine("\nDownloaded file saved in the following file system folder:\n\t" + Application.StartupPath);
Dim remoteUri As String = "http://www.contoso.com/library/homepage/images/"
Dim fileName As String = "ms-banner.gif"
Dim myStringWebResource As String = Nothing
' Create a new WebClient instance.
Dim myWebClient As New WebClient()
' Concatenate the domain with the Web resource filename. Because DownloadFile
'requires a fully qualified resource name, concatenate the domain with the Web resource file name.
myStringWebResource = remoteUri + fileName
Console.WriteLine("Downloading File ""{0}"" from ""{1}"" ......." + ControlChars.Cr + ControlChars.Cr, fileName, myStringWebResource)
' The DownloadFile() method downloads the Web resource and saves it into the current file-system folder.
myWebClient.DownloadFile(myStringWebResource, fileName)
Console.WriteLine("Successfully Downloaded file ""{0}"" from ""{1}""", fileName, myStringWebResource)
Console.WriteLine((ControlChars.Cr + "Downloaded file saved in the following file system folder:" + ControlChars.Cr + ControlChars.Tab + Application.StartupPath))
注解
DownloadFile方法从参数中指定的 URI 下载到本地文件数据 address 。The DownloadFile method downloads to a local file data from the URI specified by in the address parameter. 此方法在下载资源时阻止。This method blocks while downloading the resource. 若要下载资源并在等待服务器响应时继续执行,请使用其中一种 DownloadFileAsync 方法。To download a resource and continue executing while waiting for the server's response, use one of the DownloadFileAsync methods.
如果该 BaseAddress 属性不是空字符串 ( "" ) 并且不 address 包含绝对 uri, address 则必须是与组合的相对 uri, BaseAddress 才能形成所请求数据的绝对 uri。If the BaseAddress property is not an empty string ("") and address does not contain an absolute URI, address must be a relative URI that is combined with BaseAddress to form the absolute URI of the requested data. 如果该 QueryString 属性不是空字符串,则将其追加到 address 。If the QueryString property is not an empty string, it is appended to address.
此方法使用 RETR 命令下载 FTP 资源。This method uses the RETR command to download an FTP resource. 对于 HTTP 资源,使用 GET 方法。For an HTTP resource, the GET method is used.
备注
当你在应用程序中启用网络跟踪后,此成员将输出跟踪信息。This member outputs trace information when you enable network tracing in your application. 有关详细信息,请参阅 .NET Framework 中的网络跟踪。For more information, see Network Tracing in .NET Framework.
在中间层应用程序中使用此方法时,例如 ASP.NET 页,如果执行应用程序的帐户没有访问该文件的权限,则会收到错误。When using this method in a middle tier application, such as an ASP.NET page, you will receive an error if the account under which the application executes does not have permission to access the file.