通过与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内的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的有毒混合或在我的项目的一些腐败现象,这是晦涩或东西。精彩。
解决方案
提示:
不使用
HttpContext.Current.ApplicationInstance.CompleteRequest();
代替,使用
Response.Close();
如果您使用的是前者,你会得到HTML垃圾添加到您的zip文件的底部。
其他提示
你有没有试图把在AddFile方法有一些虚拟的文本=我认为这是必需的。
在链接到CodePlex上的例子似乎说AddEntry
方法从流读取数据。你只是传递一个字符串? - 也许你可以尝试创建一个StringReader
看你ReadmeText字符串,相反在通过这个
不隶属于 StackOverflow