-->

通过与DotNetZip响应发送压缩文件给客户(Sending Zip file to Client

2019-09-19 15:43发布

这是我的代码

    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文件与上写着“一些文本”在文档中的zip内的readme.txt文件。

我得到的是一个zip文件名为filename.zip(预期)与文档的readme.txt(预期)与doucment(意外)的内部没有任何文字。

此代码是从示例几乎一字不差这里 。 这使我的事情其他人也遇到了这个确切的问题。

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

    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文件,但我会满足于获得例如现在的工作。

任何人有什么建议?

谢谢

--UPDATE--这应该工作,其实如果我把它复制到一个新的项目,它的工作原理就像广告,我必须有dll的有毒混合或在我的项目的一些腐败现象,这是晦涩或东西。 精彩。

Answer 1:

暗示:

不要使用

HttpContext.Current.ApplicationInstance.CompleteRequest();    

相反,使用

Response.Close();

如果您使用的是前者,你会得到追加到您的zip文件的底部HTML垃圾。



Answer 2:

您是否尝试过在AddFile方法有一些虚拟的文字把=我认为这是必需的。



Answer 3:

你挂在CodePlex上的例子似乎说AddEntry方法从流中读取数据。 你只是传递一个字符串-也许你可以尝试创建一个StringReader看你ReadmeText字符串,相反在通过这个?



文章来源: Sending Zip file to Client via Response with DotNetZip