我與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();
}
注意:這是我的第一篇文章,所以不要在生氣我如果我做錯了什麼!
這是在'Index'行動的唯一代碼? – haim770
這是可以重現問題的實際代碼嗎?因爲您只是將現有的zip文件讀入內存,然後不使用它。所以這不應該是一個問題。 –