user1273357
user1273357

Reputation:

File Download from Azure Blob

Below code is for donwnloading file from azure blob. I have problem with .docx,.xlsx files and that too after deployment only, mean in local machine it is working fine.

The problem is after downloading .xlsx or .docx, when i open that file showing file corrupted popup.

public void DownloadBlob(string blobName)
{
    //You have to get values for below items from azure
    string accountName = "MyAccName";
    string accountPrimaryKey = "MyKey";
    string blobContainer = "ContainerName";
    CloudStorageAccount account = new CloudStorageAccount(new StorageCredentialsAccountAndKey(accountName, accountPrimaryKey), false);
    CloudBlobClient blobClient = account.CreateCloudBlobClient();
    CloudBlobContainer container = blobClient.GetContainerReference(blobContainer);
    CloudBlob blob = container.GetBlobReference(blobName);
    MemoryStream memStream = new MemoryStream();
    blob.DownloadToStream(memStream);

    Response.ContentType = blob.Properties.ContentType;
    Response.AddHeader("Content-Disposition", "Attachment; filename=" + blobName.ToString());
    Response.AddHeader("Content-Length", (blob.Properties.Length - 1).ToString());
    Response.BinaryWrite(memStream.ToArray());
    Response.End();
}

Upvotes: 0

Views: 5430

Answers (1)

AvkashChauhan
AvkashChauhan

Reputation: 20571

I am sure you have issue with your code as As Steve suggested you are setting length incorrectly.

I worked on similar issue sometime last year and documented the solution in my blog as below:

http://blogs.msdn.com/b/avkashchauhan/archive/2011/04/05/downloading-word-and-excel-files-from-windows-azure-storage-in-a-asp-net-web-role.aspx

Upvotes: 1

Related Questions