Downloading and saving a File from Windows Azure BLOB storage

In this post I will show you to download a file from Azure BLOB.

Set the Connection String as below,

image

Function to download and save file is as below,


public void  DownloadFileFromBlob(string fileName, string containerName,string storageConnectionString)
        {
            account = CloudStorageAccount.Parse(RoleEnvironment.GetConfigurationSettingValue(storageConnectionString));
            blobClient = account.CreateCloudBlobClient();
            container = blobClient.GetContainerReference(containerName);
            blob = container.GetBlobReference(fileName);
            MemoryStream memStream = new MemoryStream();
            blob.DownloadToStream(memStream);
            Response.ContentType = blob.Properties.ContentType;
            Response.AddHeader("Content-Disposition", "Attachment; filename=" + fileName.ToString());
            Response.AddHeader("Content-Length", blob.Properties.Length.ToString());
            Response.BinaryWrite(memStream.ToArray());

        }

you need to pass

  1. Connection string to BLOB storage
  2. Public Container name
  3. BLOB name or file name

Thanks for reading. I hope this post was useful Smile


	

Published by

Dhananjay Kumar

Dhananjay Kumar is Developer Evangelist for Infragistics. He is a 8 times Microsoft MVP and well respected Developer Advocate in India.He is the author of 900+ Blog Posts, and can often be found speaking around India at conferences and hosting free workshops for programmers across the country. So far, he has hosted 60 free workshops on various topics like JavaScript, Angular, WCF, ASP.NET MVC, C#, Azure etc. Follow him on twitter @debug_mode for all the updates about his blog posts and workshops. You can send him email at debugmode [at] outlook [dot] com

2 thoughts on “Downloading and saving a File from Windows Azure BLOB storage”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s