无法下载文件。给予例外

时间:2019-06-26 10:13:41

标签: c# azure zip

我正在尝试使用zipOutputStream下载zip formate中的多个文件。 blob.DownloadToStream(zipOutputStream);中的给定异常线

List<string> lstPath = DAL_AttachmentSQLHelper.GetAllAttachementPath(claimId);            
            CloudStorageAccount cloudStorageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));
            var container = cloudStorageAccount.CreateCloudBlobClient().GetContainerReference("traveoappclaimattachments/Traveo1");           
            using (var zipOutputStream = new ZipOutputStream(System.Web.HttpContext.Current.Response.OutputStream))
            {
                foreach (var blobFileName in lstPath)
                {
                    zipOutputStream.SetLevel(0);
                    var blob = container.GetBlockBlobReference(blobFileName);
                    var entry = new ZipEntry(blobFileName);
                    zipOutputStream.PutNextEntry(entry);
                    blob.DownloadToStream(zipOutputStream);
                }
                zipOutputStream.Finish();
                zipOutputStream.Close();
            }
            System.Web.HttpContext.Current.Response.BufferOutput = false;
            System.Web.HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + "zipFileName.zip");
            System.Web.HttpContext.Current.Response.ContentType = "application/octet-stream";
            System.Web.HttpContext.Current.Response.Flush();
            System.Web.HttpContext.Current.Response.End();

1 个答案:

答案 0 :(得分:0)

我在您的代码中看到的一个问题是您如何在代码中创建CloudBlobContainer的实例。假设您的容器名称为traveoappclaimattachments,并且所有文件都位于Traveo1中,则可以尝试以下操作:

List<string> lstPath = DAL_AttachmentSQLHelper.GetAllAttachementPath(claimId);            
CloudStorageAccount cloudStorageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));
var container = cloudStorageAccount.CreateCloudBlobClient().GetContainerReference("traveoappclaimattachments");           
using (var zipOutputStream = new ZipOutputStream(System.Web.HttpContext.Current.Response.OutputStream))
{
    foreach (var blobFileName in lstPath)
    {
        zipOutputStream.SetLevel(0);
        var blob = container.GetBlockBlobReference("Traveo1/" + blobFileName);
        var entry = new ZipEntry(blobFileName);
        zipOutputStream.PutNextEntry(entry);
        blob.DownloadToStream(zipOutputStream);
    }
    zipOutputStream.Finish();
    zipOutputStream.Close();
}
System.Web.HttpContext.Current.Response.BufferOutput = false;
System.Web.HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + "zipFileName.zip");
System.Web.HttpContext.Current.Response.ContentType = "application/octet-stream";
System.Web.HttpContext.Current.Response.Flush();
System.Web.HttpContext.Current.Response.End();