piotrwalat.net

Downloading files in Windows 8 apps using Background Transfer feature

3 comments

In this blog post I am going to show how to use Background Transfer feature to download files over HTTP in a Windows Store C#/XAML app. Background Transfer has several advantages over using HttpClient and is much better for long running transfers. I am going to create a simple app, that initiates download over the Internet, tracks progress of the download and supports re-attaching transfers after the app is closed.

 

HttpClient vs Background Transfer

C# developers writing Windows Store apps have two main options to download files from network locations over HTTP:

  • HttpClient class (and its GetAsync method)
  • Background Transfer functionality

Using HttpClient is only viable when dealing with relatively small files (MSDN suggests ‘a couple of KB’). By default HttpClient response content buffer size is set to 64KB and if response is bigger than we would get an error. Buffer size can be increased if necessary (MaxResponseContentBufferSize property), but if you need to do that it is likely that you should use Background Transfer feature instead.

Background Transfer has several other advantages. First of all it runs outside of calling application which means that if the app is suspended, the download can still continue in the background. It also provides inherent support for pause, resume operations and can cope with sudden network status changes automatically. When app terminates, existing downloads will paused and persisted. Moreover it also plays nicely with power management (OS can disable downloads when it deems it necessary) and metered networks (via BackgroundDownloader.CostPolicy property) – after all user may not want to download 1GB+ file over roamed data connection. These two features are very important in mobile scenarios and make Background Transfer even more appealing.

 

 Starting a download

Let’s create a simple C#/XAML app that will download a big file from a remote location and store it in the local app data store. Start off with a blank Windows Store app template and modify MainPage.xaml to look like this.

The interface is very simple and consists of a button to initiate download as well as a progress bar to indicate progress.

Here is DownloadClick event handler implementation.

First of all we need to obtain StorageFile (or more precisely IStorageFile implementation) – in our scenario we use ApplicationData.Current.LocalFolder.CreateFileAsync() to create a file in local app data store. Please note that we use async/await pattern, hence the event handling method is marked as async. We also need to create BackgroundDownloader class instance that is used to actually create new download using CreateDownload() method.

Every download created using Background Tranfser feature is encapsulated in DownloadOperation object. These objects provide basic operations used to manipulate the download. In example above we start the download using StartAsync() that returns IAsyncOperationWithProgress. We make use of AsTask() extension method to cast the returned value to Task and provide progress callback used to update ProgressBar control.

 

Handling existing downloads

Previous example is a very simple scenario. Once we terminate the app, we lose track of our download, even though its being paused and persisted by Background Transfer automatically. To address this problem we can use BackgroundDownloader.GetCurrentDownloadsAsync() method to retrieve all active DownloadOperation objects for our applciation. Once we do this we can easily re-attach progress handler and any logic handling completed downloads.

Once we have this code in place our download will reattach every time we load the page. For simplicity we support only one active download.

You can find the source on bickbucket .

Written by Piotr Walat

October 1st, 2012 at 9:02 am