Sending Zip file to Client via Response with DotNetZip

冷暖自知 提交于 2019-12-07 16:52:02

问题


this is my code

    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();
    }

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.

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();
    }

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

Anyone have any suggestions?

Thanks

--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.


回答1:


hint:

don't use

HttpContext.Current.ApplicationInstance.CompleteRequest();    

instead, use

Response.Close();

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




回答2:


Have you tried putting in the AddFile method with some dummy text = I think this is required.




回答3:


The example you linked to on CodePlex seems to say the AddEntry method reads data from a stream. You're just passing in a string - maybe you could try creating a StringReader to look at your ReadmeText string, and pass this in instead?



来源:https://stackoverflow.com/questions/1153047/sending-zip-file-to-client-via-response-with-dotnetzip

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!