/// <summary> /// Downloads the file locally /// </summary> /// <param name="file"></param> /// <returns></returns> public async Task DownloadFileAsync(OneDriveFile file) { // Prepare the picker suggisting the same OneDrive file name var picker = new FileSavePicker { SuggestedFileName = file.Name, SuggestedStartLocation = PickerLocationId.Downloads, DefaultFileExtension = Path.GetExtension(file.Name), }; picker.FileTypeChoices.Add(file.Name, new List <string> { picker.DefaultFileExtension }); // Ask where save the file StorageFile storageFile = await picker.PickSaveFileAsync(); // storageFile is null if the user cancel the pick operation if (storageFile != null) { // Add a new item at the beginning of the list var item = new OneDriveFileProgress(file); _progressItems.Insert(0, item); // Start the download operation await item.DownloadFileAsync(storageFile, file.DownloadUri); } }
/// <summary> /// Uploads a new file into the folder id specificied /// </summary> /// <param name="folderId"></param> /// <returns></returns> public async Task UploadFileAsync(string folderId) { // Check if session is set if (AuthenticationService == null) { throw new InvalidOperationException($"No {nameof(AuthenticationService)} has been specified"); } // Open the picker for file selection var picker = new FileOpenPicker { SuggestedStartLocation = PickerLocationId.Downloads, }; // User can select any file picker.FileTypeFilter.Add("*"); StorageFile storageFile = await picker.PickSingleFileAsync(); // storageFile is null if no file has been selected if (storageFile != null) { // Create the graph request builder for the drive IDriveRequestBuilder driveRequest = AuthenticationService.GraphClient.Me.Drive; // If folder id is null, the request refers to the root folder IDriveItemRequestBuilder driveItemsRequest; if (folderId == null) { driveItemsRequest = driveRequest.Root; } else { driveItemsRequest = driveRequest.Items[folderId]; } try { // Create an upload session for a file with the same name of the user selected file UploadSession session = await driveItemsRequest .ItemWithPath(storageFile.Name) .CreateUploadSession() .Request() .PostAsync(); // Add a new upload item at the beginning var item = new OneDriveFileProgress(storageFile.Name); _progressItems.Insert(0, item); // Start the upload process await item.UploadFileAsync(AuthenticationService, storageFile, session); } catch (Exception ex) { Error?.Invoke(this, ex); } } }