首页 > 解决方案 > 使用 windows 社区工具包 OneDrive 服务上传文件到 OneDrive 时后台传输错误

问题描述

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

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

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

在此处输入图像描述

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

标签: uwponedrivewindows-community-toolkit

解决方案


推荐阅读