Jaa


DownloadOperation Class

Definition

Performs an asynchronous download operation. The Background transfer sample demonstrates this functionality. For an overview of Background Transfer capabilities, see Transferring data in the background.

public ref class DownloadOperation sealed : IBackgroundTransferOperationPriority
/// [Windows.Foundation.Metadata.ContractVersion(Windows.Foundation.UniversalApiContract, 65536)]
/// [Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
class DownloadOperation final : IBackgroundTransferOperationPriority
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.Foundation.UniversalApiContract), 65536)]
[Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
public sealed class DownloadOperation : IBackgroundTransferOperationPriority
Public NotInheritable Class DownloadOperation
Implements IBackgroundTransferOperationPriority
Inheritance
Object Platform::Object IInspectable DownloadOperation
Attributes
Implements

Windows requirements

Device family
Windows 10 (introduced in 10.0.10240.0)
API contract
Windows.Foundation.UniversalApiContract (introduced in v1.0)
App capabilities
internetClient internetClientServer privateNetworkClientServer

Examples

Sample applications that use this class include the Background transfer sample.

The following example demonstrates how to configure and begin a basic download operation.

using Windows.Foundation;
using Windows.Networking.BackgroundTransfer;
using Windows.Storage;

private async void StartDownload_Click(object sender, RoutedEventArgs e)
{
    try
    {
        Uri source = new Uri(serverAddressField.Text.Trim());
        string destination = fileNameField.Text.Trim();

        StorageFile destinationFile = await KnownFolders.PicturesLibrary.CreateFileAsync(
            destination, CreationCollisionOption.GenerateUniqueName);

        BackgroundDownloader downloader = new BackgroundDownloader();
        DownloadOperation download = downloader.CreateDownload(source, destinationFile);

        // Attach progress and completion handlers.
        HandleDownloadAsync(download, true);
    }
    catch (Exception ex)
    {
        LogException("Download Error", ex);
    }
}

Remarks

At next start-up after application termination, your application should enumerate all existing DownloadOperation instances using GetCurrentDownloadsAsync. When a UWP application using Background Transfer is terminated, incomplete downloads persist in the background. If your application is restarted after termination and these incomplete operations are not enumerated and re-introduced to the current session, then they will go stale and continue to occupy device resources.

Background transfer doesn't support concurrent downloads of the same Uri. So an app can download http://example.com/myfile.wmv once, or download it again after a previous download completed. An app shouldn't start two downloads of the same Uri concurrently, since this may result in truncated files.

Note

Paused or incomplete download operations can only be resumed if the server accepts range-requests.

Timeout considerations

  1. When establishing a new connection for a download over TCP/SSL, the connection attempt is aborted if not established within five minutes.
  2. After the connection has been established, an HTTP request message that has not received a response within two minutes is aborted. Assuming there is Internet connectivity, Background Transfer will retry a download up to three times. In the event Internet connectivity is not detected, additional attempts will not be made until it is.

Debugging Guidance

Stopping a debugging session in Microsoft Visual Studio is comparable to closing your app; downloads are paused and POST uploads are terminated. Even while debugging, your app should enumerate and then pause, resume, restart, or cancel any persisted downloads.

However, if Microsoft Visual Studio project updates, like changes to the app manifest, require that the app is uninstalled and re-deployed for debugging, GetCurrentDownloadsAsync cannot enumerate persisted operations created using the previous app deployment.

Version history

Windows version SDK version Value added
1709 16299 CurrentWebErrorStatus
1709 16299 GetDownloadedRanges
1709 16299 GetResultRandomAccessStreamReference
1709 16299 IsRandomAccessRequired
1709 16299 RangesDownloaded
1709 16299 RecoverableWebErrorStatuses
1803 17134 MakeCurrentInTransferGroup
2004 19041 RemoveRequestHeader
2004 19041 SetRequestHeader

Properties

CostPolicy

Gets and sets the cost policy for the download.

CurrentWebErrorStatus

A transfer's WebErrorStatus, which can be monitored during the lifetime of the DownloadOperation.

Group

Note

Group may be altered or unavailable for releases after Windows 8.1. Instead, use TransferGroup.

Gets a string value indicating the group the transfer belongs to.

Guid

This is a unique identifier for a specific download operation. A GUID associated to a download operation will not change for the duration of the download.

IsRandomAccessRequired

A boolean property to enable random access. The property must be set to TRUE before calling StartAsync() on a DownloadOperation object to use the random access feature. After calling StartAsync(), call GetResultRandomAccessStreamReference to get a reference to the random access stream and read from it.

Method

Gets the method to use for the download.

Priority

Gets or sets the transfer priority of this download operation when within a BackgroundTransferGroup. Possible values are defined by BackgroundTransferPriority.

Progress

Gets the current progress of the operation.

RecoverableWebErrorStatuses

A set of WebErrorStatus values that applications anticipate the download will hit, and that the applications know how to handle.

RequestedUri

Gets the URI from which to download the file.

ResultFile

Returns the IStorageFile object provided by the caller when creating the DownloadOperation object using CreateDownload.

TransferGroup

Gets the group that this download operation belongs to.

Methods

AttachAsync()

Returns an asynchronous operation that can be used to monitor progress and completion of the attached download. Calling this method allows an app to attach download operations that were started in a previous app instance.

GetDownloadedRanges()

Returns the full list of file ranges that have been downloaded so far.

GetResponseInformation()

Gets the response information.

GetResultRandomAccessStreamReference()

Gets a reference to the random access stream and reads from it. It's necessary to set IsRandomAccessRequired to TRUE before calling GetResultRandomAccessStreamReference.

GetResultStreamAt(UInt64)

Gets the partially downloaded response at the specified position.

MakeCurrentInTransferGroup()

Prioritizes the download transfer operation (and any transfers that follow it in the same transfer group). Calling this method on a transfer that doesn't belong to a transfer group has no effect.

Pause()

Pauses a download operation.

RemoveRequestHeader(String)

Used to remove an HTTP request header.

Resume()

Resumes a paused download operation.

SetRequestHeader(String, String)

Used to set an HTTP request header.

StartAsync()

Starts an asynchronous download operation.

Events

RangesDownloaded

Provides access to incremental download progress.

Applies to

See also