Question

I am developing Windows Phone 8 app which collect the contact from phone and store it in xml file. I want to upload it to skydrive in background. I tried this

IsolatedStorageFileStream fileStream = null;
using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
{
    fileStream = store.OpenFile("XMLFile1.xml", FileMode.Open, FileAccess.Read);
    var res = await client.UploadAsync("me/skydrive", "XMLFile1.xml", fileStream, OverwriteOption.Overwrite);
    fileStream.Close();
}

This code works perfect. But when I press home key uploading gets stop. So how can I upload file to skydrive in background even if user press screen lock key or home key? And also want to know how can I upload file to a particular folder in skydive? Folders like Documents or Pictures. How Can I use client.BackgroundUploadAsync? How can I pass fileStream object ?

Was it helpful?

Solution

You are using UploadAsync and it's cancelled when you leave your App. Because when you hit StartButton you are Navigating away from your App and as MSDN says:

When the user navigates forward, away from an app, after the Deactivated event is raised, the operating system will attempt to put the app into a dormant state. In this state, all of the application’s threads are stopped and no processing takes place, but the application remains intact in memory.

So your App gets Deactivation event and all threads and tasks are stopped. Everything should work all right if you stay in your App (as it's asynchronous).

EDIT - Downloading in background is possible by using Background Transfers

As you have spotted there is a method BackgroundUploadAsync and as it is said there:

Begins uploading a file from Windows Phone isolated storage to Microsoft SkyDrive. The file upload should continue even if the app that starts the file upload quits or is suspended.

It is allowed to download/upload files from/to directory shared/transfers/ (only this - so before you upload, your files must be copied there). The very simple code can look like this - start uploading async:

client.BackgroundTransferPreferences = BackgroundTransferPreferences.None; // check policies for this - with this you have to have your phone powered by external source and use WiFi
try
{
   client.BackgroundUploadAsync("me/skydrive", new Uri("shared/transfers/sample.txt", UriKind.Relative), OverwriteOption.Overwrite);
}
catch { }

But you must be aware that Background Transfers have its own policies - you should really strong test your App before publishing.

Hope this helps.

OTHER TIPS

use this OneDriveChunkedUpload.cs to upload large files https://gist.github.com/ificator/3460d7b9d0bff74eb0ff

This PostCompleted event is use to upload your files in skydrive:

client.PostCompleted +=
                        new EventHandler<LiveOperationCompletedEventArgs>(CreateMyFolder_Completed);
 void CreateMyFolder_Completed(object sender, LiveOperationCompletedEventArgs e)
        {
            if (e.Error == null)
            {
               string folderID = (e.Result["id"]).ToString();
                foreach (string item in names)
                {
                    using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
                    {
                        string filename = item;
                        if (store.FileExists(filename))
                        {
                            IsolatedStorageFileStream storeStream = store.OpenFile(filename, FileMode.Open, FileAccess.Read);
                            client.UploadAsync(folderID, filename, storeStream, OverwriteOption.Overwrite);

                        }                         
                    }
                }
            }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top