public UploadAsync ( int maxTries = 3, IEnumerable options = null ) : Task |
||
maxTries | int | Number of times to retry entire session before giving up. |
options | IEnumerable | |
return | Task |
/// <inheritdoc/> public async Task <IOneDriveStorageFile> UploadFileAsync(string desiredName, IRandomAccessStream content, CreationCollisionOption options = CreationCollisionOption.FailIfExists, int maxChunkSize = -1) { int currentChunkSize = maxChunkSize < 0 ? OneDriveUploadConstants.DefaultMaxChunkSizeForUploadSession : maxChunkSize; if (currentChunkSize % OneDriveUploadConstants.RequiredChunkSizeIncrementForUploadSession != 0) { throw new ArgumentException("Max chunk size must be a multiple of 320 KiB", nameof(maxChunkSize)); } if (string.IsNullOrEmpty(desiredName)) { throw new ArgumentNullException(nameof(desiredName)); } if (content == null) { throw new ArgumentNullException(nameof(content)); } var uploadSessionUri = $"{Provider.BaseUrl}/drive/items/{OneDriveItem.Id}:/{desiredName}:/oneDrive.createSession"; var conflictBehavior = new OneDriveItemConflictBehavior { Item = new OneDriveConflictItem { ConflictBehavior = OneDriveHelper.TransformCollisionOptionToConflictBehavior(options.ToString()) } }; var jsonConflictBehavior = JsonConvert.SerializeObject(conflictBehavior); HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, uploadSessionUri) { Content = new StringContent(jsonConflictBehavior, Encoding.UTF8, "application/json") }; await Provider.AuthenticationProvider.AuthenticateRequestAsync(request).ConfigureAwait(false); var response = await Provider.HttpProvider.SendAsync(request).ConfigureAwait(false); if (!response.IsSuccessStatusCode) { throw new ServiceException(new Error { Message = "Could not create an UploadSession", Code = "NoUploadSession", ThrowSite = "UWP Community Toolkit" }); } IsUploadCompleted = false; var jsonData = await response.Content.ReadAsStringAsync().ConfigureAwait(false); _uploadSession = JsonConvert.DeserializeObject <Microsoft.OneDrive.Sdk.UploadSession>(jsonData); var streamToUpload = content.AsStreamForRead(); _uploadProvider = new Microsoft.OneDrive.Sdk.Helpers.ChunkedUploadProvider(_uploadSession, Provider, streamToUpload, maxChunkSize); var uploadedItem = await _uploadProvider.UploadAsync().ConfigureAwait(false); IsUploadCompleted = true; return(InitializeOneDriveStorageFile(uploadedItem.CopyToDriveItem())); }