2015-07-13 43 views
0



我與ASP.Net MVC 4 工作,我現在遇到一個情況我有沒有解決方案,你可以幫我個建議。
我希望我的操作可以下載帶有PDF和DOCX文件的壓縮文件。與壓縮和解的部分工作正常,但是當我返回該文件(......)我得到這個錯誤:的OutputStream不適在ASP.Net MVC 4

Exception: System.Web.HttpException 
    Message: OutputStream is not available when a custom TextWriter is used. 
    Source: System.Web 
     at System.Web.HttpResponse.get_OutputStream() 
     at System.Web.Mvc.FileContentResult.WriteFile(HttpResponseBase response) 
     at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) 
     at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) 
     at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) 
     at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) 
     at System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) 

我想提一提的是,在的FileStream返回前關閉。同樣的代碼也適用於ASP.Net Web窗體。
這是我的代碼從我Listcontroller:

[HttpGet] 
    public ActionResult Download() 
    { 
     return View(somedatatodownload); 
    } 

    [HttpPost] 
    public FileResult Index() 
    { 
     FileStream zipStream = new FileStream(path + filename, FileMode.Open); 
     long FileSize = zipStream.Length; 
     byte[] Buffer = new byte[(int)FileSize]; 
     zipStream.Read(Buffer, 0, (int)zipStream.Length); 
     zipStream.Close(); 
     return File(Buffer, System.Net.Mime.MediaTypeNames.Application.Zip, filename); 
    } 

這是從查看我的代碼:

@{ 
     Html.BeginForm("Index", "List", FormMethod.Post); 
    } 
    <div> 
     <input type="submit" name="download" value="Download all zipped" /> 
    </div> 
    @{ 
      Html.EndForm(); 
     } 


注意:這是我的第一篇文章,所以不要在生氣我如果我做錯了什麼!

+0

這是在'Index'行動的唯一代碼? – haim770

+1

這是可以重現問題的實際代碼嗎?因爲您只是將現有的zip文件讀入內存,然後不使用它。所以這不應該是一個問題。 –

回答

1

看起來這是HTML TextWriter的一個問題,你可以嘗試一個簡單的解決方法來使它工作。

<form action="List/Index" method="POST"> <div> 
    <input type="submit" name="download" value="Download all zipped" /> 
</div></form 
0

它曾與

@Html.ActionLink("Some text", "Controller", "Action that returns FileResult")