.net - C# downloading from Windows Azure Storage returns an empty file without any exceptions -
i'm trying use windows azure storage windows store app mobile services store images. made uploading work following guide:
http://www.windowsazure.com/en-us/develop/mobile/tutorials/upload-images-to-storage-dotnet/
however, couldn't find material on downloading files. couldn't find classes reference windows store version! if guide me documentation grateful.
anyway, wrote code doesn't seem work:
public static async system.threading.tasks.task downloaduserimage(sqluser userdata) { var usersfolder = await getusersfolder(); var imageuri = new uri(userdata.imageuri); var accountname = "<snip>"; var key = "<snip>"; storagecredentials cred = new storagecredentials(accountname, key); cloudblobcontainer container = new cloudblobcontainer(new uri(string.format("https://{0}/{1}", imageuri.host, userdata.containername)), cred); cloudblockblob blob = container.getblockblobreference(userdata.resourcename); var imagefile = await usersfolder.createfileasync(userdata.id.tostring() + ".jpg", creationcollisionoption.replaceexisting); using (var filestream = await imagefile.openasync(fileaccessmode.readwrite)) { try { await blob.downloadtostreamasync(filestream); } catch (exception e) { tools.handleliveexception(e); } } }
this code results in empty file creation, doesn't throw exceptions whatsoever. if paste value of imageuri browser, starts downloading file , completes download successfully. however, program not, reason.
any help, please?
apparently, i've been opening stream in wrong way. here's fix:
public static async system.threading.tasks.task downloaduserimage(sqluser userdata) { var usersfolder = await getusersfolder(); var imageuri = new uri(userdata.imageuri); var accountname = "<snip>"; var key = "<snip>"; storagecredentials cred = new storagecredentials(accountname, key); cloudblobclient client = new cloudblobclient(new uri(string.format("https://{0}", imageuri.host)), cred); cloudblobcontainer container = client.getcontainerreference(userdata.containername); var blob = await container.getblobreferencefromserverasync(userdata.resourcename); var imagefile = await usersfolder.createfileasync(userdata.id.tostring() + ".jpg", creationcollisionoption.replaceexisting); using (var filestream = await imagefile.openstreamforwriteasync()) { try { await blob.downloadtostreamasync(filestream.asoutputstream()); } catch (exception e) { tools.handleliveexception(e); } } }
it works now.
Comments
Post a Comment