使用Windows社区工具包OneDrive服务将文件上传到OneDrive时出现后台传输错误

时间:2018-06-26 13:17:58

标签: uwp onedrive windows-community-toolkit

我正在尝试使用Windows社区工具包OneDrive服务将文件上传到OneDrive。我的应用程序为前台时,我可以上传。但是当放到后台时,上传进度会暂停。

现在我想使用上传操作在后台传输。

但是我收到了400个错误请求。有什么问题吗?

enter image description here

//Background Upload
                var upload = await OneDriveServiceHelper.CreateBackgroundUploadForItemAsync(oneDriveAppFolder, file, CreationCollisionOption.ReplaceExisting);
                upload.Priority = BackgroundTransferPriority.High;
                Progress<UploadOperation> progressCallback = new Progress<UploadOperation>(UploadProgress);
                //CancellationToken token = default(CancellationToken);
                CancellationTokenSource cts = new CancellationTokenSource();
                await upload.StartAsync().AsTask(cts.Token, progressCallback);


public static async Task<UploadOperation> CreateBackgroundUploadForItemAsync(OneDriveStorageFolder destinationFolder, StorageFile sourceFile, CreationCollisionOption creationCollisionOption, BackgroundTransferCompletionGroup completionGroup = null, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (destinationFolder == null)
            {
                throw new ArgumentNullException(nameof(destinationFolder));
            }

            if (sourceFile == null)
            {
                throw new ArgumentNullException(nameof(sourceFile));
            }

            //var fileCreateNew = await destinationFolder.CreateFileAsync(desiredName, CreationCollisionOption.OpenIfExists);
            return await Task.Run(
                async () =>
                {
                    var requestMessage = OneDriveService.Instance.Provider.GraphProvider.Drive.Items[destinationFolder.OneDriveItem.Id].Content.Request().GetHttpRequestMessage();
                    await OneDriveService.Instance.Provider.GraphProvider.AuthenticationProvider.AuthenticateRequestAsync(requestMessage).AsAsyncAction().AsTask(cancellationToken);
                    var uploader = completionGroup == null ? new BackgroundUploader() : new BackgroundUploader(completionGroup);
                    foreach (var item in requestMessage.Headers)
                    {
                        uploader.SetRequestHeader(item.Key, item.Value.First());
                    }
                    uploader.SetRequestHeader("Filename", sourceFile.Name);
                    return uploader.CreateUpload(requestMessage.RequestUri, sourceFile);
                }, cancellationToken);
        }

0 个答案:

没有答案