BackgroundDownloader class

5 out of 9 rated this helpful - Rate this topic

Used to configure downloads prior to the actual creation of the download operation using CreateDownload. For an overview of Background Transfer capabilities, see Transferring data in the background. Download the Background Transfer sample for examples in JavaScript, C#, and C++.

Note  Background Transfer is primarily designed for long-term transfer operations for resources like video, music, and large images. For short-term operations involving transfers of smaller resources (i.e. a couple KB), the use of provided HTTP APIs (XHR in JavaScript, HttpClient in .NET, and IXHR2 in C++) is recommended.

Syntax


var backgroundDownloader = new Windows.Networking.BackgroundTransfer.BackgroundDownloader();

Attributes

ActivatableAttribute(NTDDI_WIN8)
MarshalingBehaviorAttribute(Agile)
StaticAttribute(Windows.Networking.BackgroundTransfer.IBackgroundDownloaderStaticMethods, NTDDI_WIN8)
ThreadingAttribute(Both)
VersionAttribute(NTDDI_WIN8)

Members

The BackgroundDownloader class has these types of members:

Constructors

The BackgroundDownloader class has these constructors.

ConstructorDescription
BackgroundDownloader Instantiates a new BackgroundDownloader object.

 

Methods

The BackgroundDownloader class has these methods. With C#, Visual Basic, and C++, it also inherits methods from the Object class.

MethodDescription
CreateDownload(Uri, IStorageFile) Initializes a DownloadOperation object that contains the specified Uri and the file that the response is written to.
CreateDownload(Uri, IStorageFile, IStorageFile) Initializes a DownloadOperation object with the resource Uri, the file that the response is written to, and the request entity body.
CreateDownloadAsync Creates an asynchronous download operation that includes a URI, the file that the response will be written to, and the IInputStream object from which the file contents are read.
GetCurrentDownloadsAsync() Returns a collection of pending downloads that are not associated with a group.
GetCurrentDownloadsAsync(String) Returns a collection of pending downloads for a specific Group.
SetRequestHeader Used to set an HTTP request header.

 

Properties

The BackgroundDownloader class has these properties.

PropertyAccess typeDescription

CostPolicy

Read/writeGets or sets the cost policy for the background download operation.

Group

Read/writeGets or sets a string value indicating the group the transfer will belong to.

Method

Read/writeGets or sets the HTTP method used for the background download

ProxyCredential

Read/writeGets or sets the proxy credentials for the background transfer.

ServerCredential

Read/writeGets or sets the credentials to use to authenticate with the origin server.

 

Remarks

Download operations via FTP are supported. However, for FTP operations, authentication credentials must be provided within the specified URI. For example, ftp://user:password@server/file.txt.

After app termination, an app should enumerate all existing DownloadOperation instances at next start-up using GetCurrentDownloadsAsync. When a Windows Store app using Background Transfer is terminated, incomplete downloads will persist in the background. If the app is restarted after termination and these incomplete operations are not enumerated and re-attached to the current session, they will go stale and continue to occupy device resources.

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 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.

Examples

The following example demonstrates how to configure and begin a basic download operation, and is based on the Background Transfer sample offered in the Windows Sample Gallery.


        var download = null;
        var promise = null;

        function DownloadFile (uriString, fileName) {
            try {
                // Asynchronously create the file in the pictures folder.
                Windows.Storage.KnownFolders.picturesLibrary.createFileAsync(fileName, Windows.Storage.CreationCollisionOption.generateUniqueName).done(function (newFile) {
                    var uri = Windows.Foundation.Uri(uriString);
                    var downloader = new Windows.Networking.BackgroundTransfer.BackgroundDownloader();

                    // Create a new download operation.
                    download = downloader.createDownload(uri, newFile);

                    // Start the download and persist the promise to be able to cancel the download.
                    promise = download.startAsync().then(complete, error, progress);
                }, error);
            } catch (err) {
                displayException(err);
            }
        };

Requirements

Minimum supported client

Windows 8 [Windows Store apps only]

Minimum supported server

Windows Server 2012 [Windows Store apps only]

Namespace

Windows.Networking.BackgroundTransfer
Windows::Networking::BackgroundTransfer [C++]

Metadata

Windows.winmd

Capabilities

internetClient
internetClientServer
privateNetworkClientServer

See also

Quickstart: Download a file
Background Transfer sample

 

 

Build date: 12/4/2012

Did you find this helpful?
(1500 characters remaining)
© 2013 Microsoft. All rights reserved.