【问题标题】:WP8 upload file to skydrive in BackgroundWP8后台上传文件到skydrive
【发布时间】:2014-01-30 07:01:18
【问题描述】:

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

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?并且还想知道如何我可以将文件上传到跳伞中的特定文件夹?文件夹,如文档或图片。如何使用client.BackgroundUploadAsync?如何传递fileStream 对象?

【问题讨论】:

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


    【解决方案1】:

    您正在使用 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.

    因此,您的 App 会收到 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 - 您应该在发布之前对您的应用进行严格测试。

    希望这会有所帮助。

    【讨论】:

    • 但是当我回到 app.我的应用程序崩溃了。它在 SkyDrive_WP8_Upload_and_Download_Sample.UploadPage.d__22.MoveNext() 中给出错误 System.Threading.Tasks.TaskCanceledException: A task was canceled. at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter1.GetResult() --- 从先前抛出异常的位置结束堆栈跟踪`
    • 它可以是 - 因为您的传输已被操作系统停止(取消)(当您离开应用程序时)并且您没有处理取消(您可以提供自己的 TaskCancellationSource msdn.microsoft.com/en-us/library/live/…),返回后,您的应用会尝试执行下一个可能会引发异常的操作。
    • 应用处于活动状态后能否恢复?
    • 当用户停用应用程序时,是否有任何选项可以在后台上传它。 BackgroundUploadAsync有什么用
    • 我正在尝试使用BackgroundUploadAsync,但我该如何使用fileStream
    【解决方案2】:

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

    【讨论】:

    • 请注意 link-only answers 是不鼓励的,所以答案应该是寻找解决方案的终点(而不是另一个参考中途停留,随着时间的推移往往会变得陈旧)。请考虑在此处添加独立的概要,保留链接作为参考
    【解决方案3】:

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

    【讨论】:

    • 上传到后台使用backgroundUploadAsynk方法
    • 我没有看到任何 PostCompleted 事件。我只看到 PostAsync。如何使用 backgroundUploadAsynk ?
    • 私有 LiveConnectClient 客户端;
    • 使用 LiveConnectClient 对象查找此方法
    • 我在我的项目中使用了相同的
    猜你喜欢
    • 2013-01-07
    • 1970-01-01
    • 1970-01-01
    • 2012-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-04
    相关资源
    最近更新 更多