【发布时间】:2013-05-11 05:33:17
【问题描述】:
我一直在尝试让 .NET 4.5 (System.IO.Compression.ZipArchive) 中包含的“新”ZipArchive 在 ASP.NET 站点中工作。但它似乎不喜欢写入HttpContext.Response.OutputStream的流。
我下面的代码示例会抛出
System.NotSupportedException:不支持指定的方法
一旦尝试在流上写入。
流上的CanWrite 属性返回true。
如果我将 OutputStream 与指向本地目录的文件流交换,它就可以工作。什么给了?
ZipArchive archive = new ZipArchive(HttpContext.Response.OutputStream, ZipArchiveMode.Create, false);
ZipArchiveEntry entry = archive.CreateEntry("filename");
using (StreamWriter writer = new StreamWriter(entry.Open()))
{
writer.WriteLine("Information about this package.");
writer.WriteLine("========================");
}
堆栈跟踪:
[NotSupportedException: Specified method is not supported.]
System.Web.HttpResponseStream.get_Position() +29
System.IO.Compression.ZipArchiveEntry.WriteLocalFileHeader(Boolean isEmptyFile) +389
System.IO.Compression.DirectToArchiveWriterStream.Write(Byte[] buffer, Int32 offset, Int32 count) +94
System.IO.Compression.WrappedStream.Write(Byte[] buffer, Int32 offset, Int32 count) +41
【问题讨论】:
-
你是在开发环境还是服务器上试用
-
我目前在本地运行,所以是开发环境。我从 IHttpHandler 获取 HttpContext。
-
好的,MVC 还是 Web 表单?
-
网络表单。遗憾的是,我在项目中使用 MVC 受到限制。
-
你可以尝试线性编码,你可能缺少 response.contenttype 等;与此同时,我将尝试重新创建错误并回复您。快乐编码
标签: c# .net-4.5 outputstream httpcontext asp.net-4.5