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.

operations—it is possible to create a bunch of temporary files from the single source. If you’re up to a<br />

challenge, I invite to you write such a routine and post it somewhere for the rest of us to see!<br />

But there is an easier path using createUploadFromStreamAsync, through which you can create<br />

separate UploadOperation objects for different segments of the stream. Given a StorageFile for the<br />

source, start by calling its openReadAsync method, the result of which is an<br />

Irandom-AccessStreamWithContentType object. Through its getInputStreamAt method you then<br />

obtain an IInputStream for each starting point in the stream (that is, at each offset depending on your<br />

segment size). You then create an UploadOperation with each input stream by using<br />

create-UploadFromStreamAsync. The last requirement is to tell that operation to consume only some<br />

portion of that stream. You do this by calling its setRequestHeader("content-length", ) where<br />

is the size of the segment plus the size of other data in the request; you’ll also want to add a<br />

header to identify the segment for that particular upload. After all this, call each operation’s startAsync<br />

method to begin its transfer.<br />

Multipart Uploads<br />

In addition to the createUpload and createUploadFromStreamAsync methods, the BackgroundUploader<br />

provides another method called createUploadAsync (with three variants) that handles what are called<br />

multipart uploads.<br />

From the server’s point of view, a multipart upload is a single HTTP request that contains various<br />

pieces of information (the parts), such as app identifiers, authorization tokens, and so forth, along with<br />

file content, where each part is possibly separated by a specific boundary string. Such uploads are used<br />

by online services like Flickr and YouTube, each of which accepts a request with a multipart<br />

Content-Type. (See Content-type: multipart for a reference.) For example, as shown on Uploading<br />

Photos – POST Example, Flickr wants a request with the content type of multipart/form-data, followed<br />

by parts for api_key, auth_token, api_sig, photo, and finally the file contents. With YouTube, as<br />

described on YouTube API v2.0 – Direct Uploading, it wants a content type of multipart/related with<br />

parts containing the XML request data, the video content type, and then the binary file data.<br />

The background uploader supports all this through the BackgroundUploader.create-UploadAsync<br />

method. (Note the Async suffix that separates these from the synchronous createUpload.) There are<br />

three variants of this method. The first takes the server URI to receive the upload and an array of<br />

BackgroundTransferContentPart objects, each of which represents one part of the upload. The<br />

resulting operation will send a request with a content type of multipart/form-data with a random GUID<br />

for a boundary string. The second variation of createUploadAsync allows you to specify the content type<br />

directly (through the sub-type, such as related), and the third variation then adds the boundary string.<br />

That is, assuming parts is the array of parts, the methods look like this:<br />

var uploadOpPromise1 = uploader.createUploadAsync(uri, parts);<br />

var uploadOpPromise2 = uploader.createUploadAsync(uri, parts, "related");<br />

var uploadOpPromise3 = uploader.createUploadAsync(uri, parts, "form-data", "-------123456");<br />

649

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

Saved successfully!

Ooh no, something went wrong!