Save File from URL Using C#

To save a file from URL in C#, create an empty HTMLDocument, build a RequestMessage for the target URL, send it with document.Context.Network.Send(request), check the response status, and write the response bytes to a local file.

This article shows a C# example that you can use to save a file from URL. When you download a file from a URL, you can access, share, save, and use the file from the web for different purposes depending on your needs.

There are a few reasons why you may want to save files:

Save a File from URL in C#

To save a file from URL, you need to know its URL and have a network operations handler suitable for its protocol. Aspose.HTML for .NET provides convenient functionality for processing URLs with different kinds of protocols. To use it, create an empty HTML document and call the network request handler, as shown in the following C# example.

To save a file from URL in C#:

  1. Use the HTMLDocument() constructor to create a blank document.
  2. Create a URL with the path to the resource you want to save.
  3. Use the RequestMessage(url) constructor to create a new RequestMessage instance that represents an HTTP request message.
  4. Use the Context.Network.Send(request) method to send the request.
  5. Check whether the response is successful before reading the content.
  6. If the response is successful, use File.WriteAllBytes() to save the downloaded file to the local file system.

Let’s look at how to save a file from URL using the Aspose.HTML C# library:

 1// Download file from URL using C#
 2
 3// Create a blank document; it is required to access the network operations functionality
 4using HTMLDocument document = new HTMLDocument();
 5
 6// Create a URL with the path to the resource you want to download
 7Url url = new Url("https://docs.aspose.com/html/images/handlers/message-handlers.png");
 8
 9// Create a file request message
10using RequestMessage request = new RequestMessage(url);
11
12// Download file from URL
13using ResponseMessage response = document.Context.Network.Send(request);
14
15// Check whether response is successful
16if (response.IsSuccess)
17{
18    // Save file to a local file system
19    File.WriteAllBytes(Path.Combine(OutputDir, url.Pathname.Split('/').Last()), response.Content.ReadAsByteArray());
20}

Common Mistakes and Fixes

ProblemCauseSolution
Downloaded file is emptyThe response body is read without checking the response status.Always check whether the response is successful before reading content.
404 error with a valid-looking URLThe URL contains unencoded spaces, special characters, or an incorrectly resolved relative path.Encode URL parts when needed and use the page base URL when resolving relative links.
OutOfMemoryException with large filesThe entire response is loaded into memory with a byte array.Stream the response and copy it in chunks using a buffer.
Saved file has the wrong extensionThe URL path or redirect does not match the actual content type.Check response headers and choose the local file name or extension intentionally.
Request fails in a server or container environmentNetwork access, proxy settings, certificates, or allowed URLs are restricted.Verify network permissions, proxy configuration, and CA certificates in the runtime environment.

FAQ

Can I download files using HTTP POST?

Yes. Set request.Method = HttpMethod.Post and add content with request.Content = new StringContent(...) before sending the request.

How do I add custom headers such as API keys?

Use request.Headers.Add("Header-Name", "value") before sending the request.

Does Aspose.HTML support FTP downloads?

The built-in network layer is intended for HTTP and HTTPS workflows. FTP requires a custom network implementation outside the standard example shown on this page.

Can I use this approach for large files?

For large files, avoid loading the entire response into memory. Stream the response content and copy it to a file in chunks.

Other Platforms

Related Data Extraction Articles

You can download files from URLs using complete C# examples from the Aspose.HTML for .NET GitHub repository.