Skip to content

Add a more convenient way to download file #135

Open

Description

@Jericho

The FluentHttpClient already has all the necessary ingredients to enable a developer to download a file from a given URI but it's not easy to figure out how to do it and it involves writing several lines of code. Additionally, there is no built-in way to report progress when a large file is being downloaded.

I suggest we make the scenario of downloading a file much easier by adding an extension method which would take care of downloading content from a given URI, saving the downloaded data to a file and, optionally, reporting progress as the data is being downloaded with a single line of code. Something along the lines of await client.DownloadFileAsync(...);.

The improvement I am envisioning would enable developers to download a file with the following code:

var client = new FluentClient("https://proof.ovh.net/");

var resource = "files/1Mb.dat"; // this is the resource we want to download
var filePath = Path.Combine(Path.GetTempPath(), "1Mb.dat"); // this is the path where we want to save the downloaded file
var bufferSize = 4096; // this is the size of the buffer we will use to read the response stream
var progress = new Progress<double>(percent =>
{
    Debug.WriteLine($"Download progress: {percent}%");
}); // this is the progress reporter that will be invoked during the download

// Download the resource and save it locally with a single line of code:
await client.DownloadFileAsync(resource, filePath, bufferSize, progress);

I will be happy to submit a PR for review.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions