Azure Storage Download Issue in c#

I'm trying to download a text file from azure storage blob.     Instead I get the content of the file, plus the entire HTML of the page you download it from.    I'm really frustrated.   Help please.

            
public void DownloadFileFromBlob(string url,string fileName, string containerName)
            {
                CloudStorageAccount account = CloudStorageAccount.Parse(
                    CloudConfigurationManager.GetSetting("Storage"));
                CloudBlobClient blobClient = account.CreateCloudBlobClient();
                CloudBlobContainer container = blobClient.GetContainerReference(containerName);
                CloudBlockBlob blob = container.GetBlockBlobReference(fileName);
                SharedAccessBlobPolicy sasConstraints = new SharedAccessBlobPolicy();
                sasConstraints.SharedAccessExpiryTime = DateTime.UtcNow.AddHours(1);
            sasConstraints.Permissions = SharedAccessBlobPermissions.Read; 



            MemoryStream memStream = new MemoryStream();
            //blob.DownloadToFile()
                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());

            }

Open in new window

LVL 2
C_ParlatoAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

MlandaTCommented:
Add a Response.End(); after the Response.BinaryWrite(memStream.ToArray());
MemoryStream will still use your RAM, dminishing your scaliability. I would suggest writing to the Response.OutputStream directly. Something like blob.DownloadToStream(Response.OutputStream); Also set Response.Buffer to false. This article walks you through the concet: http://blog.degree.no/2012/04/downloading-blobs-from-windows-azure/
When you stream a file and use the Content-Disposition to specify the filename, it is advisable to enclose the file in double quotes. Helps especially if the filename has spaces
public void DownloadFileFromBlob(string url,string fileName, string containerName)
{

	CloudStorageAccount account = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("Storage"));
	CloudBlobClient blobClient = account.CreateCloudBlobClient();
	CloudBlobContainer container = blobClient.GetContainerReference(containerName);
	CloudBlockBlob blob = container.GetBlockBlobReference(fileName);
	
	SharedAccessBlobPolicy sasConstraints = new SharedAccessBlobPolicy();
	sasConstraints.SharedAccessExpiryTime = DateTime.UtcNow.AddHours(1);
	sasConstraints.Permissions = SharedAccessBlobPermissions.Read; 

	//IE needs URL encoded fileName. Important when there are spaces and other non-ansi chars in fileName.
	if (HttpContext.Current.Request.UserAgent != null && HttpContext.Current.Request.UserAgent.ToUpper().Contains("MSIE"))
		fileName = HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8).Replace("+", " ");

	Response.Buffer = false;
	Response.ContentType = blob.Properties.ContentType;
	Response.AddHeader("Content-Disposition", "Attachment; fileName=\"" + fileName.ToString() + "\"");
	Response.AddHeader("Content-Length", blob.Properties.Length.ToString());
	Response.ContentType = "application/octet-stream";
	Response.Flush();
	
	blob.DownloadToStream(Response.OutputStream);
	
	Response.End();

}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Éric MoreauSenior .Net ConsultantCommented:
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.