Home > C#, WinRT > How to monitor upload/download SkyDrive operations with Live SDK for Windows Store apps

How to monitor upload/download SkyDrive operations with Live SDK for Windows Store apps

02/07/2013

When we need to handle uploads and downloads of files from SkyDrive, it is a good practice to provide a feedback about the progress of the operations.

Using the BackgroundUploadAsync and BackgroundDownloadAsync methods of Live SDK for Windows Store apps this can be easily accomplished. We just need to define an implementation of the IProgress<T> interface, using the LiveOperationProgress class. Take a look to the following code:

var picker = new FileOpenPicker();
picker.FileTypeFilter.Add(".png");
picker.FileTypeFilter.Add(".jpeg");
picker.FileTypeFilter.Add(".jpg");
picker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;

var file = await picker.PickSingleFileAsync();
if (file != null)
{
    var client = new LiveConnectClient(App.Session);

    Progress<LiveOperationProgress> uploadProgress = new Progress<LiveOperationProgress>(
        (p) =>
        {
            Debug.WriteLine("{0}/{1} bytes uploaded ({2}%)",
                p.BytesTransferred.ToString("0,0"),
                p.TotalBytes.ToString("0,0"),
                p.ProgressPercentage.ToString("0,0"));
        });

    var opResult = await client.BackgroundUploadAsync(folderId, file.Name, file, 
        OverwriteOption.Overwrite, 
        CancellationToken.None, 
        uploadProgress);
}

In this example, we create a FileOpenPicker to select a file, then we upload it to SkyDrive using the BackgroundUploadAsync method of LiveConnectClient.

On line 12, we define a Progress action that will be invoked by the library to notify operation updates. In this example, we simply show the progress on the Output window, but we can for example show this information in the UI, so that the user knows how much the operation takes to complete. Then, on line 24, we pass this handler to the BackgroundUploadAsync method. When we execute the code, we will obtain something like this:

262,144/550,449 bytes uploaded (48%)
393,216/550,449 bytes uploaded (71%)
524,288/550,449 bytes uploaded (95%)
550,449/550,449 bytes uploaded (100%)

What we have said is valid also for the BackgroundDownloadAsync method.

Categories: C#, WinRT
%d bloggers like this: