【问题标题】:"Specified method is not supported" error with iTextSharpiTextSharp 出现“不支持指定的方法”错误
【发布时间】:2011-09-09 18:01:45
【问题描述】:

我正在使用iTextSharp 生成 PDF。我在下面添加了一个测试方法,它可以创建一个包含一个段落的简单页面。它可以工作,生成 PDF,但是,在 PDF 发送到浏览器后的某个时间,我在事件日志中收到 NotSupportedException(或者如果我自己从 Application_Error 中捕获它们)。以下是导致此错误的最简单代码:

public FileStreamResult TestPdf()
{
  using (var ms = new MemoryStream())
  {
    using (var document = new Document())
    {
      PdfWriter.GetInstance(document, ms);
      document.Open();
      document.Add(new Paragraph("Hello World!"));
      document.Close();
    }
    Response.ContentType = "application/pdf";
    Response.AddHeader("content-disposition", "attachment;filename=test.pdf");
    Response.Buffer = true;
    Response.Clear();
    Response.OutputStream.Write(ms.GetBuffer(), 0, ms.GetBuffer().Length);
    Response.OutputStream.Flush();
    Response.End();
  }
  return new FileStreamResult(Response.OutputStream, "application/pdf");
}

以及它抛出的错误(请求完成后的某个时间)

Exception information: 
Exception type: NotSupportedException 
Exception message: Specified method is not supported.
at 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__DisplayClass1c.<InvokeActionResultWithFilters>b__19()
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation)
at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass1c.<>c__DisplayClass1e.<InvokeActionResultWithFilters>b__1b()
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult)
at System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName)
at System.Web.Mvc.Controller.ExecuteCore()
at System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext)
at System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext)
at System.Web.Mvc.MvcHandler.<>c__DisplayClass6.<>c__DisplayClassb.<BeginProcessRequest>b__5()
at System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass1.<MakeVoidDelegate>b__0()
at System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.End()
at System.Web.Mvc.MvcHandler.<>c__DisplayClasse.<EndProcessRequest>b__d()
at System.Web.Mvc.SecurityUtil.<GetCallInAppTrustThunk>b__0(Action f)
at System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action)
at System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult)
at System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result)
at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

关于如何解决此问题的任何想法?是图书馆有问题还是其他什么问题?

谢谢!

编辑:

为了解决这个问题,我可以通过控制器的 File 方法使用 FileContentResult 而不是返回 FileStreamResult。这是工作代码:

public ActionResult TestPdf()
{
  using (var ms = new MemoryStream())
  {
    using (var document = new Document())
    {
      PdfWriter.GetInstance(document, ms);

      document.Open();
      document.Add(new Paragraph("Hello World!"));
      document.Close();
    }
    Response.ContentType = "application/pdf";
    Response.AddHeader("content-disposition", "attachment;filename=test.pdf");
    Response.Buffer = true;
    Response.Clear();        
    return File(ms.ToArray(), "application/pdf");
  }
}

【问题讨论】:

  • 我在一个简单的 asp 页面中测试你的代码,没有错误,一切正常!你能告诉我们你在哪里调用 TestPdf() 函数

标签: c# asp.net-mvc itextsharp notsupportedexception


【解决方案1】:

此行有错误:

Response.OutputStream.Write(ms.GetBuffer(), 0, ms.GetBuffer().Length);

使用ToArray 而不是GetBuffer。像这样:

var bytes = ms.ToArray();
Response.OutputStream.Write(bytes, 0, bytes.Length);

MemoryStream.GetBuffer 返回分配的字节,而不是填充的字节。

问题示例:

using (var memoryStream = new MemoryStream())
{
    memoryStream.WriteByte(1);
    var length = memoryStream.ToArray().Length; // returns 1
   var bufferLength = memoryStream.GetBuffer().Length; // returns 256
}

虽然增加了一个字节,GetBuffer 将返回 256 个字节。

【讨论】:

  • 不幸的是,这并没有使 NotSupportedException 异常消失,但我确实合并了您的修复。谢谢。
  • 我认为你不应该返回Response.OutputStream。如果您只是尝试在不处理 ms 的情况下返回它怎么办:HttpContext.Response.AddHeader("content-disposition", "attachment; filename=form.pdf"); return new FileStreamResult(ms, "application/pdf");
  • 我收到“无法访问已关闭的流”异常。即使我在返回之前尝试“ms.Seek(0, SeekOrigin.Begin)”,我也会得到同样的异常,所以我假设 iTextSharp 文档在某个时候关闭了流。
  • @pbz - 你是否删除了内存流周围的using
  • 是的,流已“关闭”。 .ToArray() 有效,但其他任何东西(如 Seek)都无效。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-12
  • 2021-03-12
  • 2013-03-30
  • 1970-01-01
  • 1970-01-01
  • 2015-12-26
相关资源
最近更新 更多