如何在MVC中的动作结果中返回PDF [英] How to return a PDF in an action result in MVC
问题描述
我有点问题让我解决这个问题。我有一个ajax调用,应该呈现一个加载PDF的iframe。 PDF是使用托管在其他环境中的Apache FOP生成的。到目前为止我所拥有的是:
I have a little problem getting my head around this problem. I have an ajax call that should render an iframe which loads a PDF. The PDF is generated using Apache FOP hosted in another environment. What I have so far is:
在控制器动作中(iFrame指向的src元素),代码片段为:
in the controller action (where the src element of the iFrame points), the code snippet is:
var targetStream = new MemoryStream();
using (var response = FOPrequest.GetResponse()) // response from FOP
{
using (var stream = response.GetResponseStream())
{
stream.CopyTo(targetStream);
}
}
return new FileStreamResult(targetStream, "application/pdf");
但是,这不能按预期工作。将按预期填充流,但PDF不会在iFrame中呈现。
我得到的Http响应代码为200(OK)。
However, this does not work as expected. The stream is populated as expected, but the PDF does not render in the iFrame. I get a Http response code of 200 (OK).
我很感激任何帮助。
推荐答案
你使用MVC FileContentResult 返回ActionResult
例如:
You use MVC FileContentResult to return ActionResult For example:
return File(fileArray, contentType, fileName)
另一个堆栈答案
这篇关于如何在MVC中的动作结果中返回PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!