c# - OneDrive Upload/Download to Specified Directory -


i'm trying use live sdk (v5.6) include backup/restore onedrive in windows phone 8.1 silverlight application. can read/write standard "me/skydrive" directory, having horrible time in finding way upload/download specified directory. can create folder if doesn't exist no problem.

i have been trying below no luck.

var res = await _client.uploadasync("me/skydrive/mydir", filename, isostorefilestream, overwriteoption.overwrite); 

i've tried getting directory id , passing in also.

var res = await _client.uploadasync("me/skydrive/" + folderid, filename, isostorefilestream, overwriteoption.overwrite); 

same error.. receive 'mydir' or id isn't supported...

"{request_url_invalid: microsoft.live.liveconnectexception: url contains path 'mydir', isn't supported."

any suggestions? if suggest answer uploadasync, include how download file specified directory? thanks!

share|improve question
up vote 11 down vote accepted

here's extension method checks if folder created and:

  1. if created returns folder id.
  2. if not created, creates , returns folder id.

you can use id upload , download folder.

public async static task<string> createdirectoryasync(this liveconnectclient client, string foldername, string parentfolder)     {         string folderid = null;          // retrieves 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 passed name.             if (folder.name.tolowerinvariant() == foldername.tolowerinvariant())             {                 folderid = folder.id;                 break;             }         }          if (folderid == null)         {             // directory hasn't been found, creates using postasync method.             var folderdata = new dictionary<string, object>();             folderdata.add("name", foldername);             opresult = await client.postasync(parentfolder, folderdata);             result = opresult.result;              // retrieves id of created folder.             folderid = result.id;         }          return folderid;     } 

you use as:

string skydrivefolder = await createdirectoryasync(liveconnectclient, "<yourfoldernamehere>", "me/skydrive"); 

now skydrivefolder has folder id can use when uploading , downloading. here's sample upload:

liveoperationresult result = await liveconnectclient.uploadasync(skydrivefolder, filename,                                                   filestream, overwriteoption.overwrite); 

addition complete answer ynotdraw

using provided, here's how download text file specifying file name. below not include if file not found , other potential exceptions, here works when 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"] list<object>;         string id = string.empty;          // search file - add handling here if file not found         foreach (object item in items)         {             idictionary<string, object> file = item 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;     } 

and in usage:

var result = await downloadfile(_client, "mydir", "backup.txt"); 
share|improve answer
    
thanks posting code. seeing error 'microsoft.live.liveconnectclient' not contain definition 'downloadasync'. can tell me why seeing error? – ezaspi jun 23 '14 @ 0:13
    
@ezaspi using latest version of live sdk? platform targeting? – ertay shashko jun 23 '14 @ 19:51
    
live sdk 5.6. windows phone 8.1. – ezaspi jun 23 '14 @ 21:59
    
is 8.1 silverlight or winrt? haven't tested on 8.1, if there no downloadasync should there different name. – ertay shashko jun 23 '14 @ 22:31
    
winrt. got work. had 5.6 installed downloadasync not available. went createbackgrounddownloadasync. – ezaspi jun 24 '14 @ 12:23

your answer

 
discard

posting answer, agree privacy policy , terms of service.

not answer you're looking for? browse other questions tagged or ask own question.

Comments