空的档案压缩与ZipForge多个文件和C#

问题描述:

当我有这个小码:空的档案压缩与ZipForge多个文件和C#

HttpContext.Current.Response.Clear(); 

if (files.Contains(',')) { 
    HttpContext.Current.Response.ContentType = "application/zip"; 
    HttpContext.Current.Response.AddHeader("content-disposition", "filename=arch-01.zip"); 

    string[] split = files.Split(','); 
    ZipForge zip = new ZipForge(); 
    try { 
     zip.FileName = Path.Combine(@"C:\Item", "arch-01.zip"); 
     zip.OpenArchive(System.IO.FileMode.Create); 
     zip.BaseDir = @"C:\"; 

     foreach (string file in split) { 
      zip.AddFiles(file); 
     } 

     zip.CloseArchive(); 

    } catch (Exception e) { 
     Console.Write(e.Message); 
    } 
} 

压缩,显示在浏览器Open - Save As对话框后,我想。该文件存在,我们假设files包含

C:\Item\a1.xls,C:\Item\a2.xls. 

但归档是空的,永远。我的错误在哪里?我有C:的许可。 我在响应头错了吗?

HttpContext.Current.Response.WriteFile(zip.FileName); 

和现在的作品:)

我会尝试拆分出你的问题出找出问题的所在:

问题下面的代码解决

我已经加入。制作一个控制台应用程序,用于压缩您想要压缩的文件并查看是否有效。之后,看看你是否可以下载你创建的压缩文件,并且正确地从你的asp.net网站上下载压缩文件。代码中似乎有两件事情没有问题,但我们需要确定实际问题的位置。

我猜你缺少你的响应头附加。 它应该是这样的

HttpContext.Current.Response.AddHeader(“Content-Disposition”,“attachment; filename = arch-01.zip”);

这应该可以解决我想这个问题。

+0

我知道'attachment',但它不工作无论如何 – 2012-03-14 07:11:18