Downloading Azure Blob files in MVC3

James picture James · Jul 19, 2011 · Viewed 20.3k times · Source

Our ASP.NET MVC 3 application is running on Azure and using Blob as file storage. I have the upload part figured out.

The View is going to have the File Name, which, when clicked will prompt the file download screen to appear.

Can anyone tell me how to go about doing this?

Answer

user94559 picture user94559 · Jul 20, 2011

Two options really... the first is to just redirect the user to the blob directly (if the blobs are in a public container). That would look a bit like:

return Redirect(container.GetBlobReference(name).Uri.AbsoluteUri);

If the blob is in a private container, you could either use a Shared Access Signature and do redirection like the previous example, or you could read the blob in your controller action and push it down to the client as a download:

Response.AddHeader("Content-Disposition", "attachment; filename=" + name); // force download
container.GetBlobReference(name).DownloadToStream(Response.OutputStream);
return new EmptyResult();