Partager via


Transferring data in the background (XAML)

Use the Windows.Networking.BackgroundTransfer APIs provided in the Windows Runtime to enhance your app with advanced file download and upload features that run in the background during app suspension, demonstrate per operation lifecycle management, and persist beyond app termination. This feature is ideal for large file download and upload operations using the HTTP and HTTPS protocols. FTP is supported, but only when conducting download operations.

Background Transfer runs separately from the calling app and is primarily designed for long-term transfer operations for resources like video, music, and large images. For these scenarios, using Background Transfer is essential because downloads continue to progress even when the app is suspended. On phone, background transfers continue to progress even when the app is terminated. For more information on the lifecycle of an app, review the Application lifecycle documentation.

Note   If you are downloading small resources that are likely to complete quickly, you should use HttpClient APIs instead of Background Transfer.

 

How does the Background Transfer feature work?

When an app uses Background Transfer to initiate a transfer, the request is configured and initialized using BackgroundDownloader or BackgroundUploader class objects. Each transfer operation is handled individually by the system and separate from the calling app. Progress information is available if you want to give status to the user in your app's UI, and your app can pause, resume, cancel, or even read from the data while the transfer is occurring. The way transfers are handled by the system promotes smart power usage and prevents problems that can arise when a connected app encounters events like app suspension, termination, or sudden network status changes.

Watch this brief video for a demonstration of a simple file download using Background Transfer.

Can my app use Background Transfer to create authenticated file requests?

Background Transfer provides methods that support basic server and proxy credentials, cookies, and the use of custom HTTP headers (via SetRequestHeader) for each transfer operation.

How does this feature adapt to network status changes or unexpected shutdowns?

The Background Transfer feature maintains a consistent experience for each transfer operation when network status changes occur by intelligently leveraging connectivity and carrier data-plan status information provided by the Connectivity feature. To define behavior for different network scenarios, an app sets a cost policy for each operation using values defined by BackgroundTransferCostPolicy.

For example, the cost policy defined for an operation can indicate that the operation should be paused automatically when the device is using a metered network. The transfer is then automatically resumed (or restarted) when a connection to an "unrestricted" network has been established. For more information on how networks are defined by cost, see NetworkCostType.

While the Background Transfer feature has its own mechanisms for handling network status changes, there are other general connectivity considerations for network-connected apps. Read Leveraging available network connection information for additional info.

Note  

For Windows Phone Store apps, there are features that allow the user to monitor and restrict the amount of data that is transferred based on the type of connection, roaming status, and the user's data plan. Because of this, background transfers may be paused on the phone even when the BackgroundTransferCostPolicy indicates that the transfer should proceed.

The following table indicates when background transfers are allowed on the phone for each BackgroundTransferCostPolicy value, given the current state of the phone. You can use the ConnectionCost class to determine the phone's current state.

Phone State UnrestrictedOnly Default Always
Connected to WiFi Allow Allow Allow
Metered Connection, not roaming, under data limit, on track to stay under limit Deny Allow Allow
Metered Connection, not roaming, under data limit, on track to exceed limit Deny Deny Allow
Metered Connection, roaming, under data limit Deny Deny Allow
Metered Connection, over data limit. This state only occurs when the user enables "Restrict background data in the Data Sense UI. Deny Deny Deny

 

The RequestUnconstrainedDownloadsAsync and RequestUnconstrainedUploadsAsync are not supported on phone. Calls to these APIs will compile successfully but will fail at runtime with an E_NOT_IMPL exception.

Learn more about Background Transfer

Section Description
Transferring a file from a network resource

Add the capability to securely download and upload files to your Windows Store app.

Background Transfer API reference

API documentation for the entire Background Transfer Windows Runtime namespace.

Background Transfer sample

This sample demonstrates the power-friendly, cost-aware, and flexible behavior of the Background Transfer API for Windows Store apps.

 

Connecting to an HTTP server using Windows.Web.Http