后台传输状态未更改为已完成

时间:2015-02-04 12:40:07

标签: windows-runtime windows-phone-8.1 win-universal-app background-transfer

我创建了一个Windows Phone 8.1(WinRT)应用,支持从手机上传一个或多个文件。

我为此使用了背景上传器。我使用以下功能

创建后台上传
    public async void AddUploads(IReadOnlyList<StorageFile> uploadFiles)
    {
        BackgroundTransferGroup group = BackgroundTransferGroup.CreateGroup(this.GroupId);
        group.TransferBehavior = BackgroundTransferBehavior.Serialized;

        // BackgroundUploader automatically sets Content-Type header to multipart/related
        BackgroundUploader uploader = new BackgroundUploader();
        uploader.TransferGroup = group;
        uploader.SetRequestHeader("Authorization", "Bearer " + this.AccessToken);
        uploader.Method = "POST";

        foreach (StorageFile file in uploadFiles)
        {
            UploadOperation upload = await uploader.CreateUploadAsync(new Uri(this.uploadUri, UriKind.Absolute), GetTransferContents(file), "related");

            Progress<UploadOperation> progressCallback = new Progress<UploadOperation>(UploadProgressNotification);

            // Will this exception be caught??
            try
            {
                upload.StartAsync().AsTask(this.CancelToken, progressCallback);
            }
        }
     }

我通过以下回调

更新进度的UI
    public void UploadProgressNotification(UploadOperation upload)
    {
        var ignore = Window.Current.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
        {
            var uploadProgress = upload.Progress;

            Progress = (uploadProgress.BytesSent + uploadProgress.BytesReceived) * 100.0 / (uploadProgress.TotalBytesToSend + uploadProgress.TotalBytesToReceive);

            switch(uploadProgress.Status)
            {
                case BackgroundTransferStatus.Canceled:
                    Status = "Canceled";
                    break;
                case BackgroundTransferStatus.Idle:
                    Status = "Waiting";
                    break;
                case BackgroundTransferStatus.PausedNoNetwork:
                    Status = "Waiting for network";
                    break;
                case BackgroundTransferStatus.Running:
                    Status = "Uploading";
                    break;
                case BackgroundTransferStatus.Error:
                    Status = "Failed";

                    System.Diagnostics.Debug.WriteLine(upload.GetResponseInformation().StatusCode);

                    var headers = upload.GetResponseInformation().Headers;

                    foreach(string key in headers.Keys)
                    {
                        System.Diagnostics.Debug.WriteLine(headers[key]);
                    }

                    System.Diagnostics.Debug.WriteLine(upload.GetResponseInformation().IsResumable);

                    break;
                case BackgroundTransferStatus.Completed:
                    Status = "Completed";
                    break;
                default:
                    Status = "Waiting";
                    break;
            }

            UploadStatus = uploadProgress.Status;
        });
    }

但是,即使文件成功上传到服务器并且进度为100%,大多数时候,UploadOperation的状态也不会从“正在运行”更改为“已完成”。当我上传多个文件时,其中一些显示“已完成”状态,而其他一些显示“正在运行”,但所有这些文件都已成功上传。

如何确定文件何时完全上传?

1 个答案:

答案 0 :(得分:0)

您可以使用IAsyncOperation的常规完成处理程序。

即:

private async void Foo()
{
    // ...

    var task = upload.StartAsync().AsTask(this.CancelToken, progressCallback);
    var notAwaited = task.ContinueWith(OnUploadCompleted, upload);
}

private void OnUploadCompleted(Task<DownloadOperation> task, object arg)
{
    // Upload is complete at this point.
    // Check task.Status to see if the upload was successful or not.
}