I follow your code in mvc and download csv in my site, when I open it, the content inside is not what I added but some html template.
If this is your problem, you could refer to the following code:
public ActionResult Download()
{
CloudStorageAccount cloudStorageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));
CloudBlobClient _blobClient = cloudStorageAccount.CreateCloudBlobClient();
CloudBlobContainer _cloudBlobContainer = _blobClient.GetContainerReference("data");
CloudBlockBlob _blockBlob = _cloudBlobContainer.GetBlockBlobReference("table.csv");
Response.AddHeader("Content-Disposition", "attachment; filename=" + "table.csv");
MemoryStream ms = new MemoryStream();
_blockBlob.DownloadToStream(ms);
ms.Position = 0;
return File(ms, "application/octet-stream", "table.csv");
}
Also you could return Redirect(blobUrl);
BTW, if your blob is private, you need to create a Shared Access Signature
with Read permission and Content-Disposition
header set and create blob URL based on that and use that URL. In this case, the blob contents will be directly streamed from storage to the client browser.
For more details, refer to the following code:
public ActionResult Download()
{
CloudStorageAccount account = new CloudStorageAccount(new StorageCredentials("accountname", "accountkey"), true);
var blobClient = account.CreateCloudBlobClient();
var container = blobClient.GetContainerReference("container-name");
var blob = container.GetBlockBlobReference("file-name");
var sasToken = blob.GetSharedAccessSignature(new SharedAccessBlobPolicy()
{
Permissions = SharedAccessBlobPermissions.Read,
SharedAccessExpiryTime = DateTime.UtcNow.AddMinutes(10),//assuming the blob can be downloaded in 10 miinutes
}, new SharedAccessBlobHeaders()
{
ContentDisposition = "attachment; filename=file-name"
});
var blobUrl = string.Format("{0}{1}", blob.Uri, sasToken);
return Redirect(blobUrl);
}