Download.Client

.NET lightweight library for download operations offering download statistics/metrics and events while downloading.


Keywords
download, downloader, httpclient
License
MIT
Install
Install-Package Download.Client -Version 1.0.3

Documentation

Download Client

Build status Nuget SDK Downloads on Nuget

.NET lightweight library for download operations offering download statistics/metrics and events while downloading.

Usage

Install package from Nuget by running the following command.

Package Manager Console

Install-Package Download.Client

.NET CLI

dotnet add package Download.Client

Then go ahead import the library in your target class.

using Neon.Downloader;

Initialize a new instance of download client where you can optionally set the maximum size per download in the constructor, the default is 1GB.

IDownloader _downloader = new DownloadClient();
_downloader.DownloadTrace += (string trace) => // trace event logs
_downloader.OnDownloadStart += (DownloadMetric metric) => // indicates that download has started
_downloader.DownloadCompleted += (DownloadMetric metric, Stream stream) => // last metric with downloaded stream
_downloader.OnError += (Exception ex) => // when an error occurs
_downloader.Download("http://example.com/video.mp4");

DownloadMetric

A time-series representation metric object that defines the state of a download operation/activity either before, during download, or after the download completes.

This is how each download metric looks like

    public struct DownloadMetric
    {
        /// <summary>
        /// Checks whether the time measurement/calculation unit is seconds
        /// or milliseconds. 
        /// 
        /// Returns <see cref="true"/> if it's using seconds
        /// and <see cref="false"/> if it uses milliseconds.
        /// </summary>
        public bool TimeInSeconds { get; }
        /// <summary>
        /// Received/Downloaded Bytes. (d)
        /// </summary>
        public long DownloadedBytes { get; set; }
        /// <summary>
        /// Total Bytes to download. (b)
        /// </summary>
        public long TotalBytes { get; set; }
        /// <summary>
        /// Download progress percentage. (p)
        /// 
        /// <code>
        ///     = (d / b) * 100;
        /// </code>
        /// </summary>
        public double Progress { get; }
        /// <summary>
        /// Remaining Bytes to complete download. (r)
        /// 
        /// <code>
        ///     = (b - d);
        /// </code>
        /// </summary>
        public long RemainingBytes { get; }
        /// <summary>
        /// Checks if the download is complete by looking at the size
        /// of remaining/pending bytes.
        /// </summary>
        public bool IsComplete { get; }
        /// <summary>
        /// Elapsed time at the current moment. (t)
        /// </summary>
        public TimeSpan ElapsedTime { get; set; }
        /// <summary>
        /// Download speed. (s)
        /// 
        /// <code>
        ///     = (d / t);
        /// </code>.
        /// 
        /// The result is in (bytes/sec) or (bytes/ms) depending on the unit
        /// chosen for representing time.
        /// </summary>
        public double Speed { get; }
        /// <summary>
        /// Expiration or time remaining for download to complete. (e)
        /// 
        /// <code>
        ///     = (r / s);
        /// </code>
        /// </summary>
        public TimeSpan TimeRemaining { get; }
    }