UWP uploads download files using the OneDrive service for Windows Community Toolkit

Source: Internet
Author: User

I wrote two articles at the end of last year.

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

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

Unexpectedly six months later, the VS compiler hint method is obsolete , visible Microsoft loose, gas go developers rumors, not fake??????

But the new upgraded OneDrive service is more useful, but I don't feel it??????

Here to share with you the new version of how to use.

To put the elephant, ah yuck, which has an elephant?? The To use the OneDrive API, fly has three steps???

1. Registering the Application ID

2. Authorization program function

3. Using the OneDrive API

All right, let's go.

1. Registering the Application ID

Go to https://apps.dev.microsoft.com/, next to the aggregated app, choose Add App, follow the wizard, and select "Mobile and Desktop App".

You should have it when you're done.

Note Remember this app ID and you'll need it in a minute. The best way is to collect this page.

2. Authorization program function

Open Package.appxmanifest, go to the "features" page, tick the "Private network" (client and server side).

3. Using the OneDrive API

3.0 Download NuGet Package

Open NuGet, search for Microsoft.Toolkit.Uwp.Services, install version 3.0 or above.

Friendly tips: 4.0 and it could change.

3.1 Initialization

A word.

string[] scopes = new string[] {microsoftgraphscope.filesreadwriteappfolder};
OneDriveService.Instance.Initialize ("The application ID just requested"nullnull);

Scopes is the right to use, my app only needs to use the app folder under OneDrive, so that's it. Of course, there are other permissions, such as Files.read.all,files.readwrite.all, see Microsoftgraphscope the following enumeration.

3.2 Login

The core is also a sentence

if (await  OneDriveService.Instance.LoginAsync ())                {                    await  OneDriveService.Instance.AppRootFolderAsync ();                    Tipservices.tipauthenticatesuccess ();                }                 Else                 {                    tipservices.tipauthenticatefail ();                     Throw New Exception ("unableto sign in");                }

After the login was successful, I immediately acquired the application folder under OneDrive. Other folders resolutely do not visit, do not do rogue behavior. Resolutely not to bat.

3.3 Getting files

Loop get

var await folder. Getitemsasync ();  Do {    await  folder.} Nextitemsasync ();}  while null);

3.4 Creating a Folder

string await Onedrivesamplehelpers.inputtextdialogasync ("New Folder Name"); if (! string . IsNullOrEmpty (newfoldername)) {    await  folder. Storagefolderplatformservice.createfolderasync (NewFolderName, creationcollisionoption.generateuniquename);}

3.5 Go to sub-folder

var await  await currentfolder.getitemsasync (= CurrentFolder;

3.6 Move, copy, rename items

await _onedrivestorageitem.moveasync (targetonedrivestoragefolder); await _onedrivestorageitem.copyasync (targetonedrivestoragefolder); await _onedrivestorageitem.renameasync ("NewLevel3");

3.7 Create/Upload files less than 4M

var await Openlocalfileasync (); if NULL ) {    using (varawait  selectedfile.openreadasync ())    {         var await folder. Storagefolderplatformservice.createfileasync (Selectedfile.name, Creationcollisionoption.generateuniquename, Localstream);}    }

3.8 Create/Upload files larger than 4M

varSelectedfile =awaitOpenlocalfileasync ();if(Selectedfile! =NULL)    {        using(varLocalstream =awaitSelectedfile.openreadasync ()) {Shell.Current.DisplayWaitRing=true; //If The file exceed the Maximum size (ie 4MB)            varlargefilecreated =awaitFolder. Storagefolderplatformservice.uploadfileasync (Selectedfile.name, Localstream, Creationcollisionoption.generateuniquename, the*1024x768); }    }}

I don't know why we have to differentiate, and 4M is the dividing line. It seems that Gayhub have discussed it and are interested to check it out.

3.9 Downloading files

var onedrivefile = (Toolkit.Services.OneDrive.OneDriveStorageFile) item; using (var remotestream = (await as irandomaccessstream) {    await  Savetolocalfolder (Remotestream, onedrivefile.name);}

3.10 Get thumbnail images

var file = (Toolkit.Services.OneDrive.OneDriveStorageItem) ((Appbarbutton) e.originalsource). DataContext; using (var stream = (await as irandomaccessstream) {    await"  Thumbnail");}

The API operations on OneDrive are basically here.

If you think Microsoft's OneDrive client is very slag, then you can write a OneDrive app and post it to the store.

Don't forget the @ me, I also use.

UWP uploads download files using the OneDrive service for Windows Community Toolkit

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.