c# - CloudBlob.DownloadToStream returns null -
i'm trying download file cloudblob via stream. refer article cloudblob
here code download blob
public stream downloadblobasstream(cloudstorageaccount account, string bloburi) { stream mem = new memorystream(); cloudblobclient blobclient = account.createcloudblobclient(); cloudblockblob blob = blobclient.getblockblobreference(bloburi); if (blob != null) blob.downloadtostream(mem); return mem; } and code convert byte array
public static byte[] readfully(stream input) { byte[] buffer = new byte[16 * 1024]; using (memorystream ms = new memorystream()) { int read; while ((read = input.read(buffer, 0, buffer.length)) > 0) { ms.write(buffer, 0, read); } return ms.toarray(); } } but null value. below content of streamed file.
what wrong this? please help.
edit
setting position 0 inside readfully method not allowed, put inside downloadblobasstream
this should work now:
public stream downloadblobasstream(cloudstorageaccount account, string bloburi) { stream mem = new memorystream(); cloudblobclient blobclient = account.createcloudblobclient(); cloudblockblob blob = blobclient.getblockblobreference(bloburi); if (blob != null) blob.downloadtostream(mem); mem.position = 0; return mem; }
your problem input stream pointer set end of steam (see screen shot, length , position both shows same value) that's why when read null. need set input stream pointer 0 using stream.position = 0 below:
public static byte[] readfully(stream input) { byte[] buffer = new byte[16 * 1024]; input.position = 0; // add line set input stream position 0 using (memorystream ms = new memorystream()) { int read; while ((read = input.read(buffer, 0, buffer.length)) > 0) { ms.write(buffer, 0, read); } return ms.toarray(); } }
Comments
Post a Comment