UWP uses the OneDrive cloud storage 2.x API (ii) "All-network debut"

Source: Internet
Author: User

Pick up an article on http://www.cnblogs.com/hupo376787/p/8032146.html

The previous article mentions that in order to create a completely seamless and optimal experience for users, UWP developers should also implement app settings and data across platforms

Analyzing the pros and cons of data roaming and OneDrive, I chose OneDrive in combination with the actual needs of your app.

After all, data roaming 100KB is not enough ...

This time for everyone I've been looking for, very simple use of the OneDrive 2.x API usage method.

That is hidden in the OneDrive service in the official UWP Community Toolkit Sample app

I think I've had enough of this app, and I've been looking at this OneDrive Service before, but I can't remember when I really used it.

I've used the grid Splitter, Markdown Textbox, Radialprogressbar, and so much more.

This is a very good example, the store has the download, Gayhub also has the source code

I have to say that the people who developed this app are very great ... Haha??

Here is a combination of my own "micro-identification/werecognition" code to talk to you.

1. Authorization

To access OneDrive, you first need authorization.

There are three ways of authorizing:

Onlineid, the simplest, I use this, is also recommended for UWP developers to use


Microsoft account with Client ID

Work or school account with client ID

Private Onedrivestoragefolder _appfolder = null; This is used to get the app folder under OneDrive

Private AsyncTask Signinasync (intIndexprovider =0,stringAppclientid =NULL)        {            if(!isinternetavailable ())return; Showbusy (true); Try            {                //Onlineid                if(Indexprovider = =0) {OneDriveService.Instance.Initialize (); }                //Microsoft account with Client ID                Else if(Indexprovider = =1) {OneDriveService.Instance.Initialize (Appclientid, Accountprovidertype.msa, Onedri Vescopes.appfolder|onedrivescopes.readwrite); }                //Work or school account with client ID                Else if(Indexprovider = =2) {OneDriveService.Instance.Initialize (Appclientid, Accountprovidertype.adal); }                if(awaitOneDriveService.Instance.LoginAsync ()) {_appfolder=awaitOneDriveService.Instance.AppRootFolderAsync (); Showbusy (false); }                Else{showbusy (false); Throw NewException ("unable to sign in"); }            }            Catch(serviceexception Serviceex) {varDialog =NewMessagedialog (Serviceex.message,"error!"); awaitdialog.                Showasync (); Showbusy (false); }            Catch(Exception ex) {varDialog =NewMessagedialog (ex. Message,"error!"); awaitdialog.                Showasync (); Showbusy (false); }            finally{showbusy (false); }        }

Note: When used, it is best to add the above-caught exceptions, just in case.

The next thing is, upload the download file. "I didn't do anything else, like creating a new file (clip) on OneDrive, or a thumbnail, etc., you can see the app description yourself"

I don't want to complicate simple things, and the team does the same thing, and it's easy to do. Don't believe the code you uploaded

Upload

                varawait  file. Getbasicpropertiesasync ();                 if 4 1024x768 1024x768 )                    await  onedriveservicehelper.uploadlargefileasync (file, Strbackupname, Creationcollisionoption.replaceexisting, _appfolder);                 Else                    await Onedriveservicehelper.uploadsimplefileasync (file, Strbackupname, creationcollisionoption.replaceexisting, _ Appfolder);

But this is to distinguish whether it is more than 4M, two ways of uploading, using my code to judge.

For details, please see the issues discussion above the official Gayhub.

Two prototypes of functions

Uploadsimplefileasync

 Public Static AsyncTask Uploadsimplefileasync (Onedrivestoragefolder folder) {Try            {                if(Folder! =NULL)                {                    varSelectedfile =awaitOpenlocalfileasync (); if(Selectedfile! =NULL)                    {                        using(varLocalstream =awaitSelectedfile.openreadasync ()) {                            varfilecreated =awaitfolder.                        Createfileasync (Selectedfile.name, Creationcollisionoption.generateuniquename, Localstream); }                    }                }            }            Catch(OperationCanceledException ex) {awaitOnedriveservicehelper.displaymessageasync (ex.            Message); }            Catch(serviceexception graphex) {awaitOnedriveservicehelper.displaymessageasync (graphEx.Error.Message); }            Catch(Exception ex) {awaitOnedriveservicehelper.displaymessageasync (ex.            Message); }            finally            {                            }        }

Uploadlargefileasync

 Public Static AsyncTask Uploadlargefileasync (Onedrivestoragefolder folder) {Try            {                if(Folder! =NULL)                {                    varSelectedfile =awaitOpenlocalfileasync (); if(Selectedfile! =NULL)                    {                        using(varLocalstream =awaitSelectedfile.openreadasync ()) {                            //If The file exceed the Maximum size (ie 4MB)                            varlargefilecreated =awaitFolder. Uploadfileasync (Selectedfile.name, Localstream, Creationcollisionoption.generateuniquename, the*1024x768); }                    }                }            }            Catch(OperationCanceledException ex) {awaitOnedriveservicehelper.displaymessageasync (ex.            Message); }            Catch(serviceexception graphex) {awaitOnedriveservicehelper.displaymessageasync (graphEx.Error.Message); }            Catch(Exception ex) {awaitOnedriveservicehelper.displaymessageasync (ex.            Message); }            finally            {                            }        }

You may have noticed that the official function parameters are different from mine, yes. I re-encapsulated it.

The official is var selectedfile = await Openlocalfileasync (), and the file needs to be selected manually. In my scene, is automatically select the database file upload, let the user choose, it is not appropriate

Download

varRemoteFile =await_appfolder.getfileasync (strbackupname); using(varRemotestream =awaitRemotefile.openasync ()) {                    byte[] buffer =New byte[Remotestream.size]; varLocalbuffer =awaitRemotestream.readasync (buffer. Asbuffer (), (UINT) remotestream.size, inputstreamoptions.readahead); varLocalfolder =ApplicationData.Current.LocalFolder; varMylocalfile =awaitLocalfolder.createfileasync (Sqlitehelper.facedbname, creationcollisionoption.replaceexisting);using(varLocalstream =awaitMylocalfile.openasync (Fileaccessmode.readwrite)) {                        awaitLocalstream.writeasync (Localbuffer); awaitLocalstream.flushasync ();Tipservices.tipdatadownloadfromcloudcomplete (); }

Download does not differentiate what size file, very simple

=================================================================================

Summarize

The UWP was originally a small audience, the information is few, I went through the pit, recorded, for the future use of OneDrive development help.

Using the OneDrive API 2.x process is as follows

    1. Register the app to get the app ID.
    2. use a token stream or code to flow through the specified scope to let the user log in. is the Signinasync function above.
    3. Upload download operation
    4. Log off the user (optional).

The above is the actual code share used in my "micro-recognition/werecognition" scene, if there are deficiencies, please correct me. Thank you.

UWP uses the OneDrive cloud storage 2.x API (ii) "All-network debut"

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.