3

我正在尝试使用 Live SDK (v5.6) 在我的 Windows Phone 8.1 Silverlight 应用程序中包含来自 OneDrive 的备份/恢复。我可以读/写标准的“me/skydrive”目录,但是我很难找到一种上传/下载到指定目录的方法。如果文件夹不存在,我可以创建文件夹没问题。

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

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

我也尝试过获取目录 ID 并将其传入。

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

同样的错误..我收到“mydir”或不支持 id...

“{request_url_invalid: Microsoft.Live.LiveConnectException: URL 包含不支持的路径‘mydir’。”

有什么建议么?如果您建议上传异步的答案,您能否还包括我如何从指定目录下载我的文件?谢谢!

4

1 回答 1

11

这是一个扩展方法,用于检查是否创建了文件夹,并且:

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

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

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。这是一个示例上传:

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

添加以完成 YnotDraw 的答案

使用您提供的内容,以下是如何通过指定文件名来下载文本文件。以下不包括是否找不到文件和其他潜在异常,但以下是星星正确对齐时的工作原理:

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");
于 2014-05-17T20:53:10.640 回答