繁体   English   中英

WP8将文件上传到后台的skydrive

[英]WP8 upload file to skydrive in Background

我正在开发Windows Phone 8应用程序,它从手机收集联系人并将其存储在xml文件中。 我想在background uploaduploadskydrive 我试过这个

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 keyhome key如何在后台将文件上传到skydrive 并且还想知道如何将文件上传到skydive中的特定文件夹? 文件或图片等文件夹。 我如何使用client.BackgroundUploadAsync 我怎样才能传递fileStream对象?

您正在使用UploadAsync,并在您离开应用程序时取消。 因为当你点击StartButton时,你正在离开你的应用程序,正如MSDN所说

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事件,并且所有线程和任务都将停止。 如果你留在你的应用程序中,一切都应该可以正常工作(因为它是异步的)。

编辑 - 使用背景传输可以在后台下载

正如您所发现的那样,有一种方法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 { }

但是你必须意识到后台传输有自己的策略 - 你应该在发布之前对你的应用程序进行强大的测试。

希望这可以帮助。

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

此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);

                        }                         
                    }
                }
            }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM