代码之家  ›  专栏  ›  技术社区  ›  jim

使用DotNetZip通过响应将Zip文件发送到客户端

  •  3
  • jim  · 技术社区  · 17 年前

    这是我的密码

        private void sendToClient(Dictionary<string, string> reportDic)
        {
            Response.Clear();
            Response.BufferOutput = false;
            String ReadmeText = "some text";
            Response.ContentType = "application/zip";
            Response.AddHeader("content-disposition", "filename=" + "filename.zip");
            using (ZipFile zip = new ZipFile())
            {
                zip.AddEntry("Readme.txt", ReadmeText);
                zip.Save(Response.OutputStream);
            }
            Response.Close();
        }
    

    此时,我只是试图返回一个zip文件,其中包含readme.txt文档,文档中包含“some text”字样。

    这段代码几乎与示例完全一致 here

    我的最终目标是做这样的事情。

        private void sendToClient(Dictionary<string, string> reportDic)
        {
            Response.BufferOutput = false;
            Response.ContentType = "application/zip";
            Response.AddHeader("content-dispostion", "filename=text.zip");
            Response.ContentEncoding = Encoding.Default;
            Response.Charset = "";
            using (ZipFile zip = new ZipFile())
            {
                foreach (string key in reportDic.Keys)
                {
                    zip.AddEntry(key, reportDic[key]);
                }
                zip.Save(Response.OutputStream);
            }
            Response.Close();
        }
    

    将三个字符串作为文件添加到zip文件中,但我现在只想让示例正常工作。

    有人有什么建议吗?

    谢谢

    3 回复  |  直到 16 年前
        1
  •  1
  •   Cheeso    16 年前

    提示:

    不要使用

    HttpContext.Current.ApplicationInstance.CompleteRequest();    
    

    相反,使用

    Response.Close();
    

        2
  •  0
  •   cjk    17 年前

    您是否尝试过在AddFile方法中添加一些虚拟文本=我认为这是必需的。

        3
  •  0
  •   Graham Clark    17 年前

    您在CodePlex上链接的示例似乎表明 AddEntry 方法从流中读取数据。你只是传入一个字符串——也许你可以尝试创建一个 StringReader 查看ReadmeText字符串,然后将其传入?

    推荐文章