天天看點

UWP 使用Windows Community Toolkit 的OneDrive service上傳下載下傳檔案

上一年年底寫過兩篇文章

UWP 使用OneDrive雲存儲2.x api(一)【全網首發】

UWP 使用OneDrive雲存儲2.x api(二)【全網首發】

沒想到半年之後,VS編譯提示方法已經過時了,可見微軟朝三暮四,氣走開發者的傳言,并不假😂😂😂

不過新更新後的OneDrive service更加好用了,但是我沒感覺出來🙄🙄🙄

下面就來和大家分享一下新版的使用方法。

要把大象裝,啊呸,哪有大象🐘了。要想使用OneDrive API,攏共分三步🚶‍

1. 注冊應用程式ID

2. 授權程式功能

3. 使用OneDrive API

好吧,開始

UWP 使用Windows Community Toolkit 的OneDrive service上傳下載下傳檔案

去 https://apps.dev.microsoft.com/,在已聚合的應用旁邊,選擇添加應用,按照向導走,注意選擇“Mobile and Desktop App”.

完成之後應該就有下圖了。

UWP 使用Windows Community Toolkit 的OneDrive service上傳下載下傳檔案

注意記住這個應用ID,一會要用到。最好的辦法就是收藏一下這個頁面咯。

打開 Package.appxmanifest,進入“功能”頁面,勾選   ✔專用網絡(用戶端和服務端)。如圖

UWP 使用Windows Community Toolkit 的OneDrive service上傳下載下傳檔案

3.0 下載下傳NuGet包

打開NuGet,搜尋Microsoft.Toolkit.Uwp.Services,安裝3.0版本或以上。

友情提示:4.0還有可能會變哦

UWP 使用Windows Community Toolkit 的OneDrive service上傳下載下傳檔案
UWP 使用Windows Community Toolkit 的OneDrive service上傳下載下傳檔案

3.1 初始化

一句話搞定

string[] scopes = new string[] { MicrosoftGraphScope.FilesReadWriteAppFolder};
OneDriveService.Instance.Initialize("剛才申請的那個應用ID", scopes, null, null);      

scopes是使用的權限,我的App隻需要使用OneDrive下的應用程式檔案夾,是以就是這個了。當然還有其它的權限,比如 Files.Read.All,Files.ReadWrite.All等,詳見MicrosoftGraphScope下面的枚舉。

3.2 登入

核心也是一句話

if (await OneDriveService.Instance.LoginAsync())
                {
                    OneDriveStorageFolder oneDriveAppFolder = await OneDriveService.Instance.AppRootFolderAsync();
                    TipServices.TipAuthenticateSuccess();
                }
                else
                {
                    TipServices.TipAuthenticateFail();
                    throw new Exception("Unable to sign in");
                }      

在登入成功後,我馬上擷取了OneDrive下面的應用程式檔案夾。别的檔案夾堅決不通路,不做流氓行為。堅決不向BAT看齊。

 3.3 擷取檔案

 循環擷取

var OneDriveItems = await folder.GetItemsAsync();
do
{
    OneDriveItems = await folder.NextItemsAsync();
}
while (OneDriveItems != null);      

 3.4 建立檔案夾

string newFolderName = await OneDriveSampleHelpers.InputTextDialogAsync("New Folder Name");
if (!string.IsNullOrEmpty(newFolderName))
{
    await folder.StorageFolderPlatformService.CreateFolderAsync(newFolderName, CreationCollisionOption.GenerateUniqueName);
}      

3.5 進入子檔案夾

var currentFolder = await _graphCurrentFolder.GetFolderAsync(item.Name);
OneDriveItemsList.ItemsSource = await currentFolder.GetItemsAsync(20);
_graphCurrentFolder = currentFolder;      

3.6 移動、複制、重命名項目

await _onedriveStorageItem.MoveAsync(targetonedriveStorageFolder);

await _onedriveStorageItem.CopyAsync(targetonedriveStorageFolder);

await _onedriveStorageItem.RenameAsync("NewLevel3");      

3.7 建立/上傳小于4M的檔案

var selectedFile = await OpenLocalFileAsync();
if (selectedFile != null)
{
    using (var localStream = await selectedFile.OpenReadAsync())
    {
        var fileCreated = await folder.StorageFolderPlatformService.CreateFileAsync(selectedFile.Name, CreationCollisionOption.GenerateUniqueName, localStream);
    }
}      

3.8 建立/上傳大于4M的檔案

var selectedFile = await OpenLocalFileAsync();
if (selectedFile != null)
    {
        using (var localStream = await selectedFile.OpenReadAsync())
        {
            Shell.Current.DisplayWaitRing = true;

            // If the file exceed the Maximum size (ie 4MB)
            var largeFileCreated = await folder.StorageFolderPlatformService.UploadFileAsync(selectedFile.Name, localStream, CreationCollisionOption.GenerateUniqueName, 320 * 1024);
        }
    }
}      

至于為什麼非要區分,而且是4M為分界線,我也不清楚。好像GayHub上讨論過,有興趣可以去查下。

3.9 下載下傳檔案

var oneDriveFile = (Toolkit.Services.OneDrive.OneDriveStorageFile)item;
using (var remoteStream = (await oneDriveFile.StorageFilePlatformService.OpenAsync()) as IRandomAccessStream)
{
    await SaveToLocalFolder(remoteStream, oneDriveFile.Name);
}      

3.10 擷取縮略圖

var file = (Toolkit.Services.OneDrive.OneDriveStorageItem)((AppBarButton)e.OriginalSource).DataContext;
using (var stream = (await file.StorageItemPlatformService.GetThumbnailAsync(Toolkit.Services.MicrosoftGraph.MicrosoftGraphEnums.ThumbnailSize.Large)) as IRandomAccessStream)
{
    await OneDriveSampleHelpers.DisplayThumbnail(stream, "thumbnail");
}      

 關于OneDrive的API操作基本都在這了。

如果你覺得微軟的OneDrive用戶端非常渣渣,那麼看完這個,你也完全可以寫一個OneDrive 的App,然後釋出到商店。

到時别忘記@我一下,我也用用。

UWP 使用Windows Community Toolkit 的OneDrive service上傳下載下傳檔案

作者:貓叔Vincent

    本文版權歸作者和部落格園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接配接,否則保留追究法律責任的權利。