Downloading WORD and EXCEL files from Windows Azure Storage in a ASP.NET Web Role


While opening Microsoft Office WORD and EXCEL Files before
saving from Windows Azure Storage in ASP.NET Web Role, a few partners reported
the following Error:


The file <File name> cannot be opened because there are problem with the content.




Note: When downloading PDF and other image files the problem
did not occurred and it occurred only with WORD & Excel files.


Following is the correct code to solve this
problem:


C# Code:

using System.IO;

using Microsoft.WindowsAzure;

using
Microsoft.WindowsAzure.StorageClient;

using
Microsoft.WindowsAzure.ServiceRuntime;

 

namespace WebRole

{

    public partial class _Default : System.Web.UI.Page

    {

        private
static CloudStorageAccount
account;

        private
static CloudBlobClient
blobClient;

        private
static CloudBlobContainer
container;

        private
static CloudBlob
blob;

 

        protected
void Page_Load(object
sender, EventArgs e)

        {

            DownloadBlob(“HelloWorld.docx”);

        }

 

        public
void DownloadBlob(string
blobName)

        {

            account = CloudStorageAccount.Parse(RoleEnvironment.GetConfigurationSettingValue(“DataConnectionString”));

       blobClient =
account.CreateCloudBlobClient();

       container =
blobClient.GetContainerReference(“<YOUR_CONTAINER_NAME>”);

       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.ToString());

       Response.BinaryWrite(memStream.ToArray());

        }

    }

}

 

VB.NET Code:

Imports System.IO

Imports Microsoft.WindowsAzure

Imports
Microsoft.WindowsAzure.StorageClient

Imports
Microsoft.WindowsAzure.ServiceRuntime

 

Public Class
_Default

    Inherits
System.Web.UI.Page

 

 

    Protected
Sub Page_Load(ByVal
sender As Object,
ByVal e As
System.EventArgs) Handles
Me.Load

DownloadBlob(“HelloWorld.docx”);

    End Sub

 

    Private Sub DownloadBlob(ByVal
blobName As String,
ByVal containerName As
String)

        Dim
account As CloudStorageAccount

        Dim
blobClient As CloudBlobClient

        Dim
container As CloudBlobContainer

        account =

        CloudStorageAccount.Parse(RoleEnvironment.GetConfigurationSettingValue(“DataConnectionString”))

        blobClient =
account.CreateCloudBlobClient()

        container =
blobClient.GetContainerReference(“<Your_Container_Name>”)

        Dim
blob As CloudBlob

        blob =
container.GetBlobReference(blobName)

        ‘Downloads
a stream. Errors for xls and Word.

        Dim
ms As New MemoryStream()

        Using
ms

            blob.DownloadToStream(ms)

            Response.ContentType = blob.Properties.ContentType

            Response.AddHeader(“Content-Disposition”, “Attachment; filename=” &
blobName)

            Response.AddHeader(“Content-Length”,
blob.Properties.Length)

           
Response.BinaryWrite(ms.ToArray())

        End Using

    End Sub

 

End Class

 

 

 

Comments (2)

  1. vijay lathiya says:

    is there any ways to write(store) microsoft azure translation data in word document.

    i mean i translate string from English Language -> Hindi and want to write it in word Document.

    Thanks

  2. Anup says:

    Hello Avkash Chauhan,

    Can you please tell us where the excel data will be once it's download from azure blob?

    Thanks in advance.