日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

UWP 使用OneDrive云存储2.x api(二)【全网首发】

發(fā)布時間:2023/12/10 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 UWP 使用OneDrive云存储2.x api(二)【全网首发】 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

上一篇提到為了給用戶打造一個完全無縫銜接的最佳體驗,UWP開發(fā)者最好也要實現App設置和數據的跨平臺

分析了數據漫游和OneDrive的優(yōu)缺點,結合自己App實際需要,我選擇了OneDrive。

畢竟數據漫游100KB不夠用啊。。。

?這一次給大家我千辛萬苦找來的、非常簡單的使用OneDrive 2.x api使用方法。

那就是隱藏在官方UWP Community Toolkit Sample App中的OneDrive Service中

?

?

?

?我覺得平時我看這個App已經夠多了,以前也瞄過一眼這個OneDrive Service,但是在真真使用它的時候,偏偏想不起來了。

我用過這里面的Grid Splitter、Markdown Textbox、RadialProgressBar、等等太多了

這是一個非常好的例子,商店有下載,gayhub也有源代碼

不得不說,微軟開發(fā)這個App的人員非常偉大了。。。哈哈哈😂

?

?

?下面就結合我自己的【微識別/WeRecognition】代碼來和大家說一下。

?

?1. 授權

要訪問OneDrive,首先需要授權。

授權有三種方式:

OnlineId,最簡單,我就用這個,也是推薦UWP開發(fā)者使用的


Microsoft account with client id

?

Work or school account with client id

?

private OneDriveStorageFolder _appFolder = null;這個用來獲取OneDrive下面的應用文件夾

private async Task SigninAsync(int indexProvider = 0, string appClientId = null){if (!IsInternetAvailable())return;ShowBusy(true);try{// OnlineIdif (indexProvider == 0){OneDriveService.Instance.Initialize();}//Microsoft account with client idelse if (indexProvider == 1){OneDriveService.Instance.Initialize(appClientId, AccountProviderType.Msa, OneDriveScopes.AppFolder | OneDriveScopes.ReadWrite);}//Work or school account with client idelse if (indexProvider == 2){OneDriveService.Instance.Initialize(appClientId, AccountProviderType.Adal);}if (await OneDriveService.Instance.LoginAsync()){_appFolder = await OneDriveService.Instance.AppRootFolderAsync();ShowBusy(false);}else{ShowBusy(false);throw new Exception("Unable to sign in");}}catch (ServiceException serviceEx){var dialog = new MessageDialog(serviceEx.Message, "Error!");await dialog.ShowAsync();ShowBusy(false);}catch (Exception ex){var dialog = new MessageDialog(ex.Message, "Error!");await dialog.ShowAsync();ShowBusy(false);}finally{ShowBusy(false);}}

?

?注意:用的時候,最好加上上面捕捉的那些異常,以防萬一。

?

?接下來無非就是,上傳下載文件咯?!疚覜]有做別的一些操作,比如在OneDrive上新建文件(夾),或者縮略圖等,你可以自行看那個App說明】

?

?我不想把簡單的事情搞得復雜,這個團隊做的也是這樣,能簡單就簡單。不信你上傳的代碼

?

?

?

上傳

var size = await file.GetBasicPropertiesAsync();if (size.Size >= 4 * 1024 * 1024)await OneDriveServiceHelper.UploadLargeFileAsync(file, strBackupName, CreationCollisionOption.ReplaceExisting, _appFolder);elseawait OneDriveServiceHelper.UploadSimpleFileAsync(file, strBackupName, CreationCollisionOption.ReplaceExisting, _appFolder);

?

不過這要區(qū)分一下是不是超過4M,兩種上傳方式,用我的代碼判斷一下即可。

具體為啥區(qū)分,請去看官方gayhub上面的Issues討論。

兩個函數的原型

UploadSimpleFileAsync

public static async Task UploadSimpleFileAsync(OneDriveStorageFolder folder){try{if (folder != null){var selectedFile = await OpenLocalFileAsync();if (selectedFile != null){using (var localStream = await selectedFile.OpenReadAsync()){var fileCreated = await folder.CreateFileAsync(selectedFile.Name, CreationCollisionOption.GenerateUniqueName, localStream);}}}}catch (OperationCanceledException ex){await OneDriveServiceHelper.DisplayMessageAsync(ex.Message);}catch (ServiceException graphEx){await OneDriveServiceHelper.DisplayMessageAsync(graphEx.Error.Message);}catch (Exception ex){await OneDriveServiceHelper.DisplayMessageAsync(ex.Message);}finally{}}

?

UploadLargeFileAsync

public static async Task UploadLargeFileAsync(OneDriveStorageFolder folder){try{if (folder != null){var selectedFile = await OpenLocalFileAsync();if (selectedFile != null){using (var localStream = await selectedFile.OpenReadAsync()){// If the file exceed the Maximum size (ie 4MB)var largeFileCreated = await folder.UploadFileAsync(selectedFile.Name, localStream, CreationCollisionOption.GenerateUniqueName, 320 * 1024);}}}}catch (OperationCanceledException ex){await OneDriveServiceHelper.DisplayMessageAsync(ex.Message);}catch (ServiceException graphEx){await OneDriveServiceHelper.DisplayMessageAsync(graphEx.Error.Message);}catch (Exception ex){await OneDriveServiceHelper.DisplayMessageAsync(ex.Message);}finally{}}

?

你可能注意到了,官方的函數參數和我用的不一樣,是的。我重新封裝了。

官方的是var selectedFile = await OpenLocalFileAsync();,需要手動選擇文件。在我的場景里面,是自動選擇數據庫文件上傳的,讓用戶選擇,就不合適了

?

?

?

下載

var remoteFile = await _appFolder.GetFileAsync(strBackupName);using (var remoteStream = await remoteFile.OpenAsync()){byte[] buffer = new byte[remoteStream.Size];var localBuffer = await remoteStream.ReadAsync(buffer.AsBuffer(), (uint)remoteStream.Size, InputStreamOptions.ReadAhead);var localFolder = ApplicationData.Current.LocalFolder;var myLocalFile = await localFolder.CreateFileAsync(SQLiteHelper.FaceDbName, CreationCollisionOption.ReplaceExisting); using (var localStream = await myLocalFile.OpenAsync(FileAccessMode.ReadWrite)){await localStream.WriteAsync(localBuffer);await localStream.FlushAsync();TipServices.TipDataDownloadFromCloudComplete();}

?

下載不區(qū)分什么大小文件,很簡單的

?

?=================================================================================

總結

UWP本來就是小眾,資料少之又少,我走過了坑,記錄下來,對以后用到OneDrive 開發(fā)的有所幫助。

使用OneDrive Api 2.x流程如下?

  • 注冊應用以獲取應用 ID。
  • 使用令牌流或代碼流通過指定的作用域讓用戶登錄。就是上面的?SigninAsync函數
  • 上傳下載操作
  • 注銷用戶(可選)。
  • ?

    ?

    ?

    以上就是在我的【微識別/WeRecognition】場景里面使用的實際代碼分享,如有不足之處,敬請指正。謝謝。

    總結

    以上是生活随笔為你收集整理的UWP 使用OneDrive云存储2.x api(二)【全网首发】的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。