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.

Starting a download transfer happens as follows. First create a StorageFile to receive the data<br />

(though this is not required as we’ll see later in this section). Then create a DownloadOperation object<br />

for the transfer using BackgroundDownloader.createDownload, at which point you can set its method,<br />

costPolicy, and group properties to override the defaults supplied by the Background-Downloader. The<br />

method is a string that identifies the type transfer being used (normally GET for HTTP or RETR for FTP).<br />

We’ll come back to the other two properties later in the “Setting Cost Policy” and “Grouping Multiple<br />

Transfers” sections.<br />

Once the operation is configured as needed, the last step is to call its startAsync method with your<br />

completed, error, and progress handlers: 72<br />

// Asynchronously create the file in the pictures folder (capability declaration required).<br />

Windows.Storage.KnownFolders.picturesLibrary.createFileAsync(fileName,<br />

Windows.Storage.CreationCollisionOption.generateUniqueName)<br />

.done(function (newFile) {<br />

// Assume uriString is the text URI of the file to download<br />

var uri = Windows.Foundation.Uri(uriString);<br />

var downloader = new Windows.Networking.BackgroundTransfer.BackgroundDownloader();<br />

// Create a new download operation.<br />

var download = downloader.createDownload(uri, newFile);<br />

}<br />

// Start the download<br />

download.startAsync().then(complete, error, progress);<br />

While the operation underway, the following properties provide additional information on the<br />

transfer:<br />

• requestedUri and resultFile The same as those passed to createDownload.<br />

• guid A unique identifier assigned to the operation.<br />

• progress A BackgroundDownloadProgress structure with bytesReceived,<br />

total-BytesToReceive, hasResponseChanged (a Boolean, see the getResponseInformation<br />

method below), hasRestarted (a Boolean set to true if the download had to be restarted), and<br />

status (a BackgroundTransferStatus value: idle, running, pausedByApplication,<br />

pausedCostedNetwork, pausedNoNetwork, canceled, error, and completed).<br />

A few methods of DownloadOperation can also be used with the transfer:<br />

• pause and resume Control the download in progress. We’ll talk more of these in the “Suspend,<br />

Resume, and Restart with Background Transfers” section below.<br />

• getResponseInformation Returns a ResponseInformation object with properties named<br />

headers (a collection of response headers from the server), actualUri, isResumable, and<br />

72 The code in the sample has more structure than shown here. It defines its own DownloadOperation class that<br />

unfortunately has the same name as the WinRT class, so I’m electing to omit mention of it.<br />

645

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

Saved successfully!

Ooh no, something went wrong!