0

我有应用程序的要求就像我需要在弹出窗口中将画布保存到 pdf 文档。为此,我使用第三方工具将图像数据转换为 pdf。

现在我是如何做到的:

  1. 首先,我创建了 Web 方法,它以 JSON 格式将画布数据从 UI 发送到服务器,然后将其转换为 MemoryStream。

  2. 我调用服务器按钮单击事件,将 MemoryStream 转换为 pdf 文档。它正在成功创建。

但是当我使用以下代码返回它时:

        HttpContext.Current.Response.Buffer = true;
        HttpContext.Current.Response.ClearContent();
        HttpContext.Current.Response.ClearHeaders();
        // inform the browser about the binary data format
        HttpContext.Current.Response.AddHeader("Content-Type", "application/pdf");

        // let the browser know how to open the PDF document
        HttpContext.Current.Response.AddHeader("Content-Disposition",
          String.Format("{0}; filename=HtmlToPdf.pdf;size={1}", "attachment",           pdfBuffer.Length.ToString()));
     HttpContext.Current.Response.BinaryWrite(pdfBuffer);
  //HttpContext.Current.Response.End();
HttpContext.Current.ApplicationInstance.CompleteRequest();

它给了我以下例外:

[FormatException: The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or a non-white space character among the padding characters. ]
   System.Convert.FromBase64String(String s) +0
   System.Web.UI.ObjectStateFormatter.Deserialize(String inputString) +77
   System.Web.UI.ObjectStateFormatter.System.Web.UI.IStateFormatter.Deserialize(String serializedState) +4
   System.Web.UI.Util.DeserializeWithAssert(IStateFormatter formatter, String serializedState) +37
   System.Web.UI.HiddenFieldPageStatePersister.Load() +147

[ViewStateException: Invalid viewstate. 
    Client IP: ::1
    Port: 
    Referer: http://[mysite]/Report.aspx
    Path: /Report.aspx
    User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)
    ViewState: /wEPDwUJNTc2MDY1MzA3D2QWAgIDD2QWBGYPZBYEZg9kFgxmD2QWAgIBDxYCHglpbm5lcmh0bWwFrgk8c2VsZWN0IGNsYXNzPSdzZWxlY3QnIElEPSAnZGltZW5zaW9uU2VsZWN0JyBvbkNoYW5nZT0nRXhwYW5kQWNjb3JkaW9uKHRoaXMsbGJsVGV4dCknPiA8b3B0Z3JvdXAgbGFiZWw9J1RpbWUnIGlkPSdUX1lfRGltZW5zaW9uXzEnPiA8b3B0aW9uIGlkID0nWScgdmFsdWUgPScwXzJfMV8xX0lOX1RpbWUnPlllYXJseTwvb3B0aW9uPiA8b3B0aW9uIGlkID0nTScgdmFsdWUgPScwXzJfMV8xX0lOX1RpbWUnPk1vbnRobHk8L29wdGlvbj4gPG9wdGlvbiBpZCA9J0QnIHZhbHVlID0nMF8yXzFfMV9JTl9UaW1lJz5EYWlseTwvb3B0aW9uPiA8b3B0aW9uIGlkID0nSCcgdmFsdWUgPScwXzJfMV8xX0lOX1RpbWUnPkhvdXJseTwvb3B0aW9uPiA8L29wdGdyb3VwPiA8b3B0Z3JvdXAgbGFiZWw9J0dlb2dyYXBoeScgaWQ9J0dfWV9EaW1lbnNpb25fMic...

请帮助我在这里缺少什么?

4

1 回答 1

0

这个例外似乎很有帮助:

[FormatException:输入不是有效的 Base-64 字符串,因为它包含非 base 64 字符、两个以上的填充字符或填充字符中的非空白字符。]

以这种方式尝试:

Response.AddHeader("Content-Disposition", "attachment; filename=" + FileName );
Response.AddHeader( "Content-Length", strFileSize );
Response.ContentType = "application/pdf" ;
于 2013-10-25T13:02:31.863 回答