Sending Zip file to Client via Response with DotNe

2019-05-11 17:26发布

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.

3条回答
Bombasti
2楼-- · 2019-05-11 17:37

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?

查看更多
3楼-- · 2019-05-11 17:47

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.

查看更多
叼着烟拽天下
4楼-- · 2019-05-11 17:49

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

查看更多
登录 后发表回答