23.01.2018 Views

MICROSOFT_PRESS_EBOOK_PROGRAMMING_WINDOWS_8_APPS_WITH_HTML_CSS_AND_JAVASCRIPT_PDF

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

awareness and handling connectivity. If, on the other hand, the user cannot do anything more until the<br />

transfer is complete, you might choose to use background transfer for perhaps any data larger than 10K<br />

or some other amount based on the current network speed.<br />

In any case, when you’re ready to employ background transfer in your app, the<br />

BackgroundDown-loader and BackgroundUploader objects in the<br />

Windows.Networking.BackgroundTransfer namespace will become your fast friends. Both objects have<br />

methods and properties through which you can enumerate pending transfers as well as perform general<br />

configuration of credentials, HTTP request headers, transfer method, cost policy (for metered networks),<br />

and grouping. Each individual operation is then represented by a DownloadOperation or<br />

UploadOperation object, through which you can control the operation (pause, cancel, etc.) and retrieve<br />

status. With each operation you can also set its particular credentials, cost policy, and so forth,<br />

overriding the general settings in the BackgroundDownloader and BackgroundUploader classes.<br />

Note In both download and upload cases, the connection request will be aborted if a new connection<br />

is not established within five minutes. After that, any other HTTP request involved with the transfer<br />

times out after two minutes. Background transfer will retry an operation up to three times if there’s<br />

connectivity.<br />

To see the basics of this API in action, let’s start by looking at the Background transfer sample. To run<br />

this sample you must first set up a localhost server along with a data file and an upload target page. So<br />

make sure you have Internet Information Services installed on your machine, as described in Chapter 13<br />

in the section “Using the Localhost.” Then, from an administrator command prompt, navigate to the<br />

sample’s Server folder and run the command powershell –file serversetup.ps1. This will install the<br />

necessary server-side files for the sample on the localhost, and allow you to run an additional example<br />

in this chapter’s companion content.<br />

Basic Downloads<br />

Scenario 1 of the Background transfer sample (js/downloadFile.js) lets you download an image file from<br />

the localhost server and save it to the Pictures library. By default the URI entry field is set to a specific<br />

localhost URI and the control is disabled. This is because the sample doesn’t perform any validation on<br />

the URI, a process that you should always perform in your own app. If you’d like to enter other URIs in<br />

the same, of course, just remove disabled="disabled" from the serverAddressField element in<br />

html/downloadFile.html. To see the downloader in action, it’s also helpful to locate some large image<br />

files that will take a while to transfer; your favorite search engine can help you out, or you can copy one<br />

of your own to the localhost server.<br />

The sample’s UI also provides a handful of buttons to start, cancel, pause, and resume the async<br />

operation, an essential feature for apps with background transfers. Within its progress handler, which<br />

the transfer operations support, the sample demonstrates how to display as much of the image has<br />

been transferred. You can also start multiple transfers to observe how they are all managed<br />

simultaneously.<br />

644

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!