加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – iTextSharp出现“不支持指定方法”错误

发布时间:2020-12-15 21:19:21 所属栏目:百科 来源:网络整理
导读:我正在使用 iTextSharp生成PDF.我在下面添加了一个测试方法,它创建了一个带有一个段落的简单页面.它工作,生成PDF,但是,在PDF发送到浏览器后的某个时候,我在事件日志中得到NotSupportedException(或者如果我自己从Application_Error中捕获它们).这是导致此错
我正在使用 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(),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)

关于如何解决这个问题的任何想法?库或其他什么问题?

谢谢!

编辑:

要解决这个问题,而不是返回FileStreamResult,我可以通过Controller的File方法使用FileContentResult.这是工作代码:

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");
  }
}

解决方法

这行有一个错误:

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

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

var bytes = ms.ToArray();
Response.OutputStream.Write(bytes,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个字节.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读