uwp - 使用 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);
}
解决方案
推荐阅读
- python-3.x - 壁虎 Python3。方程组
- javascript - 隐藏 div 滚动条
- c# - 当具有相似的方法代码但调用者名称发生变化时使用什么方法?
- kubernetes - 将 pod 指标拉取到外部 prometheus
- android - 安卓模拟器没有响应
- angular - Service Worker 在 Chrome 中未激活
- javascript - 如何在js时钟中添加刷新间隔
- node.js - npm 没有 sudo 就无法工作,get 找不到模块错误
- python - pandas 使用数字和字母的组合按系列对数据帧进行排序
- javascript - 使用 mongoose 和 nodejs 从数组中删除特定元素