WP8将文件上传到后台的skydrive

时间:2014-01-30 07:01:18

标签: c# windows-phone-8 isolatedstorage onedrive

我正在开发Windows Phone 8应用,该应用从手机收集联系人并将其存储在xml文件中。我想upload skydrive background 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(); } 。我试过这个

screen lock key

此代码非常完美。但是当我按下主页时,上传会停止。那么即使用户按home keyclient.BackgroundUploadAsync ,如何在后台将文件上传到skydrive?并且还想知道如何将文件上传到跳伞中的特定文件夹?文件或图片等文件夹。我如何使用fileStream?如何通过{{1}}对象?

3 个答案:

答案 0 :(得分:2)

您正在使用UploadAsync,并在您离开应用时取消。因为当你点击StartButton时,你正在离开你的应用程序而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.

因此,您的应用程序将获取Deactivation事件,并且所有线程和任务都将停止。如果你留在你的应用程序中,一切都应该可以正常工作(因为它是异步的)。

编辑 - 使用Background Transfers绿色可以在后台下载 正如你所发现的那样,有一种方法BackgroundUploadAsync,正如它所说的那样:

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.

允许从/到目录shared / transfers /下载/上传文件(仅限此 - 所以在上传之前,必须将文件复制到那里)。非常简单的代码可能如下所示 - 开始上传异步:

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 { }

但是你必须意识到背景传输有自己的policies - 你应该在发布之前对你的应用程序进行强有力的测试。

希望这会有所帮助。

答案 1 :(得分:1)

使用此OneDriveChunkedUpload.cs上传大文件 https://gist.github.com/ificator/3460d7b9d0bff74eb0ff

答案 2 :(得分:0)

此PostCompleted事件用于在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);

                        }                         
                    }
                }
            }