OneDrive上传/下载到指定目录

时间:2014-05-17 19:28:16

标签: c# live-sdk onedrive live-connect-sdk

我正在尝试使用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',不支持。”

有什么建议吗?如果你建议上传同步的答案,你还可以包括我如何从指定的目录下载我的文件?谢谢!

1 个答案:

答案 0 :(得分:11)

这是一个检查是否创建了文件夹的扩展方法:

  1. 如果创建,则返回文件夹ID。
  2. 如果未创建,则创建它并返回文件夹ID。
  3. 然后,您可以使用此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");