【发布时间】:2014-06-24 06:04:26
【问题描述】:
我想生成一个 PDF,我在我的控制器中使用以下代码来调用它:
public PdfActionResult Index()
{
return new PdfActionResult("");
}
自定义操作结果“pdfActionResult”如下所示:
public override void ExecuteResult(ControllerContext context)
{
var _pdfConverter = new PdfConverter { MediaType = "Print" };
var response = context.HttpContext.Response;
response.Clear();
response.AddHeader("Content-Type", "application/pdf");
response.AddHeader("Content-Disposition", String.Format("{0}; filename={1}.pdf;", true ? "attachment" : "inline", "bla"));
var z = context.HttpContext.Request.Url.AbsoluteUri;
z = z.Substring(0, z.IndexOf("?", System.StringComparison.Ordinal));
var b = _pdfConverter.GetPdfBytesFromUrl(z);
response.OutputStream.Write(b,0,b.Length);
response.Close();
response.Flush();
response.End();
}
我收到以下异常:
System.Web.HttpException: OutputStream is not available when a custom TextWriter is used.
我已阅读其他建议使用自定义操作结果的 SO 帖子,所以我这样做了,但没有成功。
怎么了?
【问题讨论】:
-
尝试使用 Response.BinaryWrite
-
@dotnetstep 我试过了,同样的错误。
-
代替 context.HttpContext.Response 试试 HttpContext.Current.Response。
-
@dotnetstep 也不起作用:(即使与 BinaryWrite() 结合使用。
标签: c# asp.net-mvc actionresult