23.01.2018 Views

MICROSOFT_PRESS_EBOOK_PROGRAMMING_WINDOWS_8_APPS_WITH_HTML_CSS_AND_JAVASCRIPT_PDF

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

There is also a variation of createDownload that takes a second StorageFile argument whose<br />

contents provide the body of the HTTP GET or FTP RETR request that will be sent to the server URI<br />

before the download is started. This accommodates some web sites that require you to fill out a form to<br />

start the download.<br />

Sidebar: Where Is Cancel?<br />

You might have already noticed that neither DownloadOperation nor UploadOperation has a<br />

cancellation method. So how is this accomplished? You cancel the transfer by canceling the<br />

startAsync operation—that is, call the cancel method of the promise returned by startAsync.<br />

This means that you need to hold onto the promises for each transfers you initiate.<br />

Basic Uploads<br />

Scenario 2 of the Background transfer sample (js/uploadFile.js) exercises the background upload<br />

capability, specifically sending some file (chosen through the file picker) to a URI that can receive it. By<br />

default the URI points to http://localhost/BackgroundTransferSample/upload.aspx, a page installed with<br />

the PowerShell script that sets up the server. As with Scenario 1, the URI entry control is disabled<br />

because the sample performs no validation, as you would again always want to do if you accepted any<br />

URI from an untrusted source (user input in this case). For testing purposes, of course, you can remove<br />

disabled="disabled" from the serverAddressField element in html/uploadFile.html and enter other URIs<br />

that will exercise your own upload services. This is especially handy if you run the server part of the<br />

sample in Visual Studio 2012 Express for Web where the URI will need a localhost port number as<br />

assigned by the debugger.<br />

In addition to a button to start an upload and to cancel it, the sample provides another button to<br />

start a multipart upload; we’ll talk more of this in the “Multipart Uploads” section below.<br />

In code, an upload happens very much like a download. Assuming you have a StorageFile with the<br />

contents to upload, create an UploadOperation object for the transfer with<br />

BackgroundUploader.createUpload. If, on the other hand, you have data in a stream (IInputStream),<br />

create the operation object with BackgroundUploader.createUploadFromStreamAsync instead. This<br />

can also be used to break up a large file into discrete chunks, if the server can accommodate it; see<br />

“Breaking Up Large Files” below.<br />

With the operation object in hand you can customize a few properties of the transfer, overriding the<br />

defaults provided by the BackgroundUploader. These are method (HTTP POST or PUT, or FTP STOR),<br />

costPolicy, and group. For the latter, again see “Setting Cost Policy” and “Grouping Multiple Transfers”<br />

below.<br />

Once you’re ready, then, calling the operation’s startAsync will proceed with the upload: 73<br />

73 As with downloads, the code in the sample has more structure than shown here and again defines its own<br />

647

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

Saved successfully!

Ooh no, something went wrong!