Home > Article > Backend Development > How to download file from URL in C#?
You can use the web client to download files from a URL. It is available in System.Net Namespaces.
The WebClient class provides common methods for sending or receiving data. From any local, intranet, or Internet resource identified by a URI.
Web clients can be called applications or web browsers (such as Google Chrome, Internet Explorer, Opera, Firefox, Safari), installed on your computer and used to Interacts with the web server based on user requests. It's basically a consumer application It collects processed data from the server.
The client and server are the two parts of the connection. They are two different machines. A web client requests information, while a web server is basically a designed personal computer Accepts requests from remote computers and sends the requested information. The web server is responsible for storing information for viewing via the browser The client is usually also the web host. Web host allows connection to server View the stored information.
The WebClient class in C# uses the WebRequest class to provide access to resources. WebClient instances can access data from any WebRequest descendant registered using the WebRequest.RegisterPrefix method.
Download file for download file.
WebClient Client = new WebClient (); client.DownloadFile("url","path");
Suppose we want to download images from the path "https://downloadfreeimages.jpg" And save it to the local directory of the computer, the code is as follows.
using System; using System.Net; namespace DemoApplication{ public class Program{ public static void Main(){ string url = "https://downloadfreeimages.jpg"; string savePath = @"D:\Demo\FreeImages.jpg"; WebClient client = new WebClient(); client.DownloadFile(url, savePath); Console.ReadLine(); } } }
The above example will download the image from the provided URL and save it to given path.
D:\Demo
The above is the detailed content of How to download file from URL in C#?. For more information, please follow other related articles on the PHP Chinese website!