2010-08-13 83 views
0

在MVC .NET C#我有过载onException的动作在我的基本控制器异常在mvc.net控制器

在控制器动作我已经生成使用Ionic.Zip.dll

这种情况的代码zip文件就像

foreach (var file in filesToarchive) 
{ 
    zip.AddFile(@file.FilePath); 
} 

zip.Save(Response.OutputStream); 
var rept = reports.First(); 
return File(Response.OutputStream, "plain/zip", "abc.zip"); 

虽然在下载onException的行动,它抛出异常的文件中基本控制器

的例外是:

System.Web.HttpResponseStream.Read在 的System.Web(字节[] 缓冲器,的Int32偏移的Int32计数)
在 System.Web.Mvc.FileStreamResult.WriteFile(HttpResponseBase 响应)。 Mvc.FileResult.ExecuteResult(controllerContext 上下文)在 System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(controllerContext controllerContext,的ActionResult 的ActionResult)在 System.Web.Mvc.ControllerActionInvoker。 <> c__DisplayClass14.b__11() 在 System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter 滤波器,ResultExecutingContext preContext,函数功能1 continuation) at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass14.<>c__DisplayClass16.<InvokeActionResultWithFilters>b__13() at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList 1个滤波器, 的ActionResult的ActionResult)在 System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext,String actionName)

如果任何人有任何解决方案,请给我。

感谢

回答

0

尝试使用内存流,而不是写入到响应:

using (var stream = new MemoryStream()) 
{ 
    zip.Save(stream); 
    return File(stream.ToArray(), "plain/zip", "abc.zip"); 
} 

而且我已经删除了var rept = reports.First();行我没有看到任何相关的代码片断你”已发布。

+0

感谢达林它实现了我的要求,并运作良好 但我只是想知道,我曾提到 能否请您明确把异常的原因是什么? 谢谢 – munish 2010-08-13 08:32:57

+0

我对'Ionic.Zip.dll'库不熟悉,但我想当你调用'zip.Save(Response.OutputStream)'时,它会刷新并关闭这个流,之后当你返回文件结果当它关闭时,流出该流不能被读取。采用流的'File'扩展方法尝试读取此流并将其写入响应流。 – 2010-08-13 08:42:26

+0

排除你是对的,但抛出异常后,它打开包含zip文件的下载窗口,我可以打开这些文件 所以我仍然有一些困惑。 – munish 2010-08-13 08:48:29