OneDrive上传/下载到指定目录 [英] OneDrive Upload/Download to Specified Directory

查看:360
本文介绍了OneDrive上传/下载到指定目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Live SDK(v5.6)在Windows Phone 8.1 Silverlight应用程序中包括从OneDrive备份/还原.我可以读/写到标准的"me/skydrive"目录,但是在寻找一种上载/下载到指定目录的方法时,我的时间很糟.如果该文件夹不存在,我可以创建该文件夹.

I'm trying to use the Live SDK (v5.6) to include backup/restore from OneDrive in my Windows Phone 8.1 Silverlight application. I can read/write to the standard "me/skydrive" directory, but I am having a horrible time in finding a way to upload/download to a specified directory. I can create the folder if it doesn't exist no problem.

我一直在下面尝试,没有运气.

I have been trying below with no luck.

var res = await _client.UploadAsync("me/skydrive/mydir", fileName, isoStoreFileStream, OverwriteOption.Overwrite);

我还尝试获取目录ID并将其也传递进来.

I've also tried getting the directory ID and passing that in also.

var res = await _client.UploadAsync("me/skydrive/" + folderId, fileName, isoStoreFileStream, OverwriteOption.Overwrite);

相同的错误.我收到"mydir"或不支持该ID ...

Same error.. I receive 'mydir' or the id isn't supported...

"{request_url_invalid:Microsoft.Live.LiveConnectException:URL包含路径"mydir",该路径不受支持."

"{request_url_invalid: Microsoft.Live.LiveConnectException: The URL contains the path 'mydir', which isn't supported."

有什么建议吗?如果您提出了关于uploadasync的答案,还可以包括如何从指定目录下载文件吗?谢谢!

Any suggestions? If you suggest an answer for the uploadasync, could you also include how I could download my file from the specified directory? Thanks!

推荐答案

这是一种扩展方法,用于检查是否创建了文件夹以及:

Here's an extension method that checks if a folder is created and:

  1. 如果创建,则返回文件夹ID.
  2. 如果未创建,则创建它并返回文件夹ID.

然后您可以使用此ID上载到该文件夹​​并从该文件夹下载.

You can then use this id to upload to and download from that folder.

public async static Task<string> CreateDirectoryAsync(this LiveConnectClient client,
string folderName, string parentFolder)
    {
        string folderId = null;

        // Retrieves all the directories.
        var queryFolder = parentFolder + "/files?filter=folders,albums";
        var opResult = await client.GetAsync(queryFolder);
        dynamic result = opResult.Result;

        foreach (dynamic folder in result.data)
        {
            // Checks if current folder has the passed name.
            if (folder.name.ToLowerInvariant() == folderName.ToLowerInvariant())
            {
                folderId = folder.id;
                break;
            }
        }

        if (folderId == null)
        {
            // Directory hasn't been found, so creates it using the PostAsync method.
            var folderData = new Dictionary<string, object>();
            folderData.Add("name", folderName);
            opResult = await client.PostAsync(parentFolder, folderData);
            result = opResult.Result;

            // Retrieves the id of the created folder.
            folderId = result.id;
        }

        return folderId;
    }

然后将其用作:

string skyDriveFolder = await CreateDirectoryAsync(liveConnectClient, "<YourFolderNameHere>", "me/skydrive");

现在skyDriveFolder具有在上载和下载时可以使用的文件夹ID.这是一个示例上传:

Now skyDriveFolder has the folder id that you can use when uploading and downloading. Here's a sample Upload:

LiveOperationResult result = await liveConnectClient.UploadAsync(skyDriveFolder, fileName,
                                                  fileStream, OverwriteOption.Overwrite);

通过YnotDraw完成答案的补充

使用您提供的内容,以下是通过指定文件名下载文本文件的方法.如果未找到该文件以及其他可能的异常,则以下内容不包括在内,但是当星标正确对齐时,这才是可行的方法:

Using what you provided, here's how to download a text file by specifying the file name. Below does not include if the file is not found and other potential exceptions, but here is what works when the stars align properly:

public async static Task<string> DownloadFileAsync(this LiveConnectClient client, string directory, string fileName)
    {
        string skyDriveFolder = await OneDriveHelper.CreateOrGetDirectoryAsync(client, directory, "me/skydrive");
        var result = await client.DownloadAsync(skyDriveFolder);

        var operation = await client.GetAsync(skyDriveFolder + "/files");

        var items = operation.Result["data"] as List<object>;
        string id = string.Empty;

        // Search for the file - add handling here if File Not Found
        foreach (object item in items)
        {
            IDictionary<string, object> file = item as IDictionary<string, object>;
            if (file["name"].ToString() == fileName)
            {
                id = file["id"].ToString();
                break;
            }
        }

        var downloadResult= await client.DownloadAsync(string.Format("{0}/content", id));

        var reader = new StreamReader(downloadResult.Stream);
        string text = await reader.ReadToEndAsync();
        return text;
    }

以及用法:

var result = await DownloadFile(_client, "MyDir", "backup.txt");

这篇关于OneDrive上传/下载到指定目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆