通过 DotNetZip 的响应将 Zip 文件发送到客户端
这是我的代码,
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 文档,其中包含“一些文本”一词。
我得到的是一个名为 filename.zip(预期)的 zip 文件,其中包含文档 readme.txt(预期),文档内没有文本(意外)。
此代码几乎逐字来自此处的示例。 这让我觉得其他人也遇到过这个问题。
我的最终目标是做这样的事情。
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 文件中,但我现在就让示例正常工作。
有人有什么建议吗?
谢谢
--更新-- 这应该有效,事实上,如果我将它复制到一个新项目中,它的工作原理就像广告中所宣传的那样,我的项目中一定有有毒的 dll 组合或一些损坏,这是晦涩难懂的。 精彩的。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
提示:
不要使用
,而是使用
如果您使用前者,您将在 zip 文件的底部附加 HTML 垃圾。
hint:
don't use
instead, use
If you use the former, you will get HTML junk appended to the bottom of your zip file.
您是否尝试过在 AddFile 方法中放入一些虚拟文本 = 我认为这是必需的。
Have you tried putting in the AddFile method with some dummy text = I think this is required.
您在 CodePlex 上链接到的示例似乎表明
AddEntry
方法从流中读取数据。 您只是传入一个字符串 - 也许您可以尝试创建一个 StringReader 来查看您的 ReadmeText 字符串,然后将其传入?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 aStringReader
to look at your ReadmeText string, and pass this in instead?