Uploads and Downloads
It is a common activity to download large amounts of data in a Windows Store app (for example, an eBook app may need to download a large file representing a book in your local library). You might also upload large files to a server, such as high-resolution pictures of the front and back of your check when you make a mobile deposit using your banking software. It is possible to perform both uploads and downloads in the background. For these scenarios, you would use the BackgroundDownloader and BackgroundUploader classes. To see an example, open the code-behind for the main page in the TapAndGoProximityNetworking project under the Chapter10 solution folder. The key step is to create a file to download to, then pass it to the helper class that will register it in the background:
var downloader = new BackgroundDownloader(); download = downloader.CreateDownload(source, destinationFile);
Your app can then launch the download task and register a method to receive progress updates:
await this.download.StartAsync().AsTask(cts.Token, progress);
If the user exits the app, the download will continue in the background. You can check to see if a background download is in process when the app is re-launched, and continue to monitor the progress:
var downloads = await BackgroundDownloader.GetCurrentDownloadsAsync(); await downloads[0].AttachAsync().AsTask(cts.Token, progress);
If the download completed in the background, it will return immediately so you can process the file. The example app downloads a video about proximity networking and launches the video once it has been retrieved. The upload experience works much the same way.