Upload file to skydrive through SkyDrive API

2019-01-24 21:00发布

问题:

I try to upload a text file to my skydrive or at least create new text file in SD and edit it's content, through SkyDrive API in my Windows 8 application. How can I do that?

I tried to do something like that:

        LiveConnectClient client = new LiveConnectClient(session);
        var fileData = new Dictionary<string, object>();
        fileData.Add("name", "new_file.txt");
        try
        {
            LiveOperationResult fileOperationResult = await client.PutAsync("me/skydrive", fileData);
            this.infoTextBlock.Text = fileOperationResult.ToString();
        }
        catch (LiveConnectException exception)
        {
            this.infoTextBlock.Text = exception.Message;
        }

but I get error "The provided request is not valid. The root SkyDrive folder cannot be updated." If I write something like "me/skydrive/" I get "The provided URL is not valid. The requested path '' is not supported". Method LiveConnectClient.PutAsync allows me only to update existing properties (but not it's content).

How it should be done properly?

Btw - Is content on LCDC(http://msdn.microsoft.com/en-us/library/live/hh826531.aspx) updated? I'm asking because some methods, which are in documentation, doesn't exist in dlls (f.e. LiveConnectClient.Upload. There's only BackgroundUploadAsync).

Thanks for help in advance, Micheal

回答1:

Close but as I wrote: I can't use client.upload method because LiveConnectClient class doesn't contain it. That's why I asked about site content update.

Anyway - I've got an answer:

//create a StorageFile (here is one way to do that if it is stored in your ApplicationData)
StorageFile file = awaitApplicationData.Current.LocalFolder.GetFileAsync("yourfilename.txt");

try {
   client = new LiveConnectClient(session);
   LiveOperationResult operationResult = await client.BackgroundUploadAsync("me/skydrive", file.Name, file, OverwriteOption.Overwrite);
}
catch (LiveConnectException exception) {
  //handle exception                
}


回答2:

You should use the Upload method on LiveConnectionClient. For example, see the Uploading Files example in the Live SDK. Something like ...

LiveOperationResult fileOperationResult =
     await client.Upload("me/skydrive", /*file name here*/, /*file stream here*/);


回答3:

Here's another way to upload a file from a console application using a SkyDriveApiClient downloaded from http://skydriveapiclient.codeplex.com/releases/view/103081

    static void Main(string[] args)
    {
        var client = new SkyDriveServiceClient();

        client.LogOn("YourEmail@hotmail.com", "password");
        WebFolderInfo wfInfo = new WebFolderInfo();

        WebFolderInfo[] wfInfoArray = client.ListRootWebFolders();

        wfInfo = wfInfoArray[0];
        client.Timeout = 1000000000;

        string fn = @"test.txt";
        if (File.Exists(fn))
        {
            client.UploadWebFile(fn, wfInfo);
        }

    }