Class MediaDownloader (1.60.0)

public class MediaDownloader : IMediaDownloader

A media downloader implementation which handles media downloads.

Inheritance

object > MediaDownloader

Implements

IMediaDownloader

Namespace

GoogleApisDownload

Assembly

Google.Apis.dll

Constructors

MediaDownloader(IClientService)

public MediaDownloader(IClientService service)

Constructs a new downloader with the given client service.

Parameter
Name Description
service IClientService

Fields

MaximumChunkSize

public const int MaximumChunkSize = 10485760

Maximum chunk size. Default value is 10*MB.

Field Value
Type Description
int

Properties

ChunkSize

public int ChunkSize { get; set; }

Gets or sets the amount of data that will be downloaded before notifying the caller of the download's progress. Must not exceed MaximumChunkSize. Default value is MaximumChunkSize.

Property Value
Type Description
int

ModifyRequest

public Action<HttpRequestMessage> ModifyRequest { get; set; }

Gets or sets the callback for modifying requests made when downloading.

Property Value
Type Description
ActionHttpRequestMessage

Range

public RangeHeaderValue Range { get; set; }

The range header for the request, if any. This can be used to download specific parts of the requested media.

Property Value
Type Description
RangeHeaderValue

ResponseStreamInterceptorProvider

public Func<HttpResponseMessage, StreamInterceptor> ResponseStreamInterceptorProvider { get; set; }

A provider for response stream interceptors. Each time a response is produced, the provider is called, and can return null if interception is not required, or an interceptor for that response. The provider itself should not read the response's content stream, but can check headers.

Property Value
Type Description
FuncHttpResponseMessageStreamInterceptor

Methods

Download(string, Stream)

public IDownloadProgress Download(string url, Stream stream)

Downloads synchronously the given URL to the given stream.

Parameters
Name Description
url string
stream Stream
Returns
Type Description
IDownloadProgress

DownloadAsync(string, Stream)

public Task<IDownloadProgress> DownloadAsync(string url, Stream stream)

Downloads asynchronously the given URL to the given stream.

Parameters
Name Description
url string
stream Stream
Returns
Type Description
TaskIDownloadProgress

DownloadAsync(string, Stream, CancellationToken)

public Task<IDownloadProgress> DownloadAsync(string url, Stream stream, CancellationToken cancellationToken)

Downloads asynchronously the given URL to the given stream. This download method supports a cancellation token to cancel a request before it was completed.

Parameters
Name Description
url string
stream Stream
cancellationToken CancellationToken
Returns
Type Description
TaskIDownloadProgress
Remarks

In case the download fails Exception will contain the exception that cause the failure. The only exception which will be thrown is TaskCanceledException which indicates that the task was canceled.

OnDataReceived(byte[], int)

protected virtual void OnDataReceived(byte[] data, int length)

Called when an HTTP response is received, allowing subclasses to examine data before it's written to the client stream.

Parameters
Name Description
data byte

Byte array containing the data downloaded.

length int

Length of data downloaded in this chunk, in bytes.

OnDownloadCompleted()

protected virtual void OnDownloadCompleted()

Called when a download has completed, allowing subclasses to perform any final validation or transformation.

OnResponseReceived(HttpResponseMessage)

protected virtual void OnResponseReceived(HttpResponseMessage response)

Called when a successful HTTP response is received, allowing subclasses to examine headers.

Parameter
Name Description
response HttpResponseMessage

HTTP response received.

Remarks

For unsuccessful responses, an appropriate exception is thrown immediately, without this method being called.

Events

ProgressChanged

public event Action<IDownloadProgress> ProgressChanged

An event which notifies when the download status has been changed.

Event Type
Type Description
ActionIDownloadProgress

Extension Method