通过与DotNetZip响应发送压缩文件给客户 [英] Sending Zip file to Client via Response with DotNetZip

查看:224
本文介绍了通过与DotNetZip响应发送压缩文件给客户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码

    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里面的.txt文件。

at this point I'm simply trying to return a zip file with the readme.txt document inside the zip with the words "some text" inside the document.

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

What I get is a zipfile named filename.zip(expected) with a document readme.txt(expected) with no text inside of the doucment(unexpected).

这代码几乎逐字从例子中的此处。这使我的事情其他人也遇到了这个确切的问题。

This code is almost verbatim from the example here. Which makes me thing other people have run into this exact problem.

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

My end goal is to do something like this.

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

add three strings as files to the zip file, but I'll settle for getting the example working for now.

任何人有什么建议?

感谢

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

--UPDATE-- This should work, in fact if I copy it into a new project, it works just as advertised, I must have a toxic mix of dlls or some corruption in my project, that is obscure or something. Wonderful.

推荐答案

提示:

不要用

HttpContext.Current.ApplicationInstance.CompleteRequest();



而不是使用

instead, use

Response.Close();

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

If you use the former, you will get HTML junk appended to the bottom of your zip file.

这篇关于通过与DotNetZip响应发送压缩文件给客户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆