从Azure Blob下载文件

时间:2012-05-08 11:10:08

标签: azure-storage-blobs

下面的代码是来自azure blob的donwnloading文件。我有.docx,.xlsx文件的问题,也只是在部署之后,意味着在本地机器上工作正常。

问题是在下载.xlsx或.docx之后,当我打开显示文件损坏弹出窗口的文件时。

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();
}

1 个答案:

答案 0 :(得分:1)

我确定您的代码存在问题,因为史蒂夫建议您设置错误的长度。

我去年某个时候处理过类似问题,并在我的博客中记录了解决方案如下:

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