0

下面的代码用于从 azure blob 下载文件。我对 .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();
}
4

1 回答 1

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-角色.aspx

于 2012-05-08T17:10:20.643 回答