开发者

Exception in mvc.net controller

In mvc .net c# i have over loaded OnException action in my base controller

In controller action I have generated zip file using Ionic.Zip.dll

The code for this is like

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");

While downloading the file it throws exception in OnException action in base controller

the exception is :

System.Web.HttpResponseStream.Read(Byte[] buffer, Int32 offset, Int32 count开发者_如何转开发)

at System.Web.Mvc.FileStreamResult.WriteFile(HttpResponseBase response) at System.Web.Mvc.FileResult.ExecuteResult(ControllerContext context) at System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass14.b__11() at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func1 continuation) at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass14.<>c__DisplayClass16.<InvokeActionResultWithFilters>b__13() at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList1 filters, ActionResult actionResult) at System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName)

If anybody has any solution then please give it to me.

Thanks


Try using a memory stream instead of writing to the response:

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

Also I've removed the var rept = reports.First(); line as I don't see any relevance to the code snippet you've posted.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜