1

I have created a Windows Phone 8.1 (WinRT) app that supports uploading one or more files from the phone.

I use Background uploader for this. I create background uploads using following function

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

I update UI for progress by following callback

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

However, most of the times even though the file is successfully uploaded to the server and progress is 100%, the status of UploadOperation does not change to "Completed" from "Running". When I am uploading multiple files, some of them show "Completed" status while others show "Running", but all of them are successfully uploaded.

How can I ascertain when the file is completely uploaded?

Shivangi Gupta
  • 866
  • 8
  • 21
  • Take a look at [this question](http://stackoverflow.com/q/27894341/2681948), I'm not sure, but maybe the answer there will help somehow. – Romasz Feb 04 '15 at 12:45

1 Answers1

0

You can use the regular completion handler of an IAsyncOperation.

i.e.:

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.
}
kiewic
  • 15,852
  • 13
  • 78
  • 101