Save File from URL – C# Example
In this article, you will see a C# example that you can use to save a file from URL. When you download file from URL, you have the ability to access, share, save, and use the file from the Web for various purposes depending on your needs. There are a few reasons why you would want to save files:
- When you save files from URLs, you can access and use those files offline when internet access is limited or unavailable.
- Saving files can help with sharing and collaboration, content distribution, or file exchange purposes.
- You want to save files for later usage or reference. This can be documents, texts, images, videos, audio, or other types of files that you want to keep for future use.
- Saving files for archiving and backup is useful for preserving the files that are important to you and protecting them from possible loss or other unforeseen events.
- You can download file from URL for learning purposes, get learning materials, and learn from anywhere, such as during commuting, travel, or in remote locations.
- and more.
How to Save File from URL
In order to save file from URL, you need to know its URL and have a network operations handler suitable for its protocol. Aspose.HTML for .NET library provides you with convenient functionality for processing URLs with different kinds of protocols. In order to use it, you just need to create an empty HTML document and call the network request handler, as shown in the following C# example:
- Use the HTMLDocument() constructor to create a blank document.
- Create a URL with the path to the resource you want to save.
- Use the
RequestMessage(url) constructor to create a new instance of the RequestMessage class, which represents an HTTP request message. The
url
parameter is passed to the constructor, specifying the URL to which the request will be sent. - Use the Context.Network.Send(request) method to send the request. The response is checked to ensure it was successful.
- Finally, if the response was successful, use the
File.WriteAllBytes()
method to save files to a local file system.
Let’s look at how to save file from URL using the Aspose.HTML C# library:
1using Aspose.Html;
2using Aspose.Html.Net;
3using System.IO;
4using System.Linq;
5...
6 // Create a blank document; it is required to access the network operations functionality
7 using var document = new HTMLDocument();
8
9 // Create a URL with the path to the resource you want to save
10 var url = new Url("https://docs.aspose.com/html/images/handlers/message-handlers.png");
11
12 // Create a file request message
13 using var request = new RequestMessage(url);
14
15 // Extract file from URL
16 using var response = document.Context.Network.Send(request);
17
18 // Check whether response is successful
19 if (response.IsSuccess)
20 {
21 // Save file to a local file system
22 File.WriteAllBytes(Path.Combine(OutputDir, url.Pathname.Split('/').Last()), response.Content.ReadAsByteArray());
23 }
You can download files from URLs using complete C# examples from GitHub.
Aspose.HTML offers HTML Web Applications that are an online collection of free converters, mergers, SEO tools, HTML code generators, URL tools, and more. The applications work on any operating system with a web browser and do not require any additional software installation. Easily convert, merge, encode, generate HTML code, extract data from the web, or analyze web pages in terms of SEO wherever you are. Use our collection of HTML Web Applications to perform your daily matters and make your workflow seamless!