Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. ASP. Net AJAX, Response.Write, and PDF

ASP. Net AJAX, Response.Write, and PDF

Scheduled Pinned Locked Moved ASP.NET
questionhelpsysadminalgorithmsjson
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Ariel Kazeed
    wrote on last edited by
    #1

    Hello everyone, I've been struggling with this for the last 6 hours, and after a lot of google-ing and research, I thought I'd ask for a little help. See: a) I have a PDF document exported to a MemoryStream. b) The control firing the event which leads to the PDF generation and exporting is inside an AJAX UpdatePanel. So all I want to do is to send the contents of that MemoryStream (containing the PDF) to the browser, so the user sees an "open or download" dialog. As far as I remember, it used to be really easy, at least before AJAX times. Now I've learnt over researching, that the AJAX framework doesn't like Response.Write() at all, but despite my searching, I haven't found a workaround yet. Here's the way I do it:

    Public Sub ExportarPDF(ByVal rpt As CrystalDecisions.CrystalReports.Engine.ReportDocument, ByVal Nombre As String)
        Try
            Dim ms As IO.MemoryStream = rpt.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat)
            Response.Clear()
            Response.Buffer = True
            Response.ContentType = "application/pdf"
            Response.AddHeader("content-disposition", "attachment; filename=" & Nombre & ".PDF")
            Response.BinaryWrite(ms.ToArray())
            Response.End()
    
        Catch ex As Exception
            Throw ex
        End Try
    End Sub
    

    And the error it returns (as far as I know, whenever you use Reponse.Write() on an AJAX postback):

    Sys.WebForms.PageRequestManagerParserErrorException" The message received from the server could not be parsed. [...] Error parsing near '%PDF-1.2
    %????
    10'

    Where the ? is actually some sort of ? with a question mark inside that I cannot find in the ascii catalog. Please, if anyone has any ideas of how can I make a browser display the open or download dialog from an AJAX postback, let me know. Thanks in advance!

    Kazz


    "Users are there to click on things, not think. Let the archs do the damn thinking."

    C 1 Reply Last reply
    0
    • A Ariel Kazeed

      Hello everyone, I've been struggling with this for the last 6 hours, and after a lot of google-ing and research, I thought I'd ask for a little help. See: a) I have a PDF document exported to a MemoryStream. b) The control firing the event which leads to the PDF generation and exporting is inside an AJAX UpdatePanel. So all I want to do is to send the contents of that MemoryStream (containing the PDF) to the browser, so the user sees an "open or download" dialog. As far as I remember, it used to be really easy, at least before AJAX times. Now I've learnt over researching, that the AJAX framework doesn't like Response.Write() at all, but despite my searching, I haven't found a workaround yet. Here's the way I do it:

      Public Sub ExportarPDF(ByVal rpt As CrystalDecisions.CrystalReports.Engine.ReportDocument, ByVal Nombre As String)
          Try
              Dim ms As IO.MemoryStream = rpt.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat)
              Response.Clear()
              Response.Buffer = True
              Response.ContentType = "application/pdf"
              Response.AddHeader("content-disposition", "attachment; filename=" & Nombre & ".PDF")
              Response.BinaryWrite(ms.ToArray())
              Response.End()
      
          Catch ex As Exception
              Throw ex
          End Try
      End Sub
      

      And the error it returns (as far as I know, whenever you use Reponse.Write() on an AJAX postback):

      Sys.WebForms.PageRequestManagerParserErrorException" The message received from the server could not be parsed. [...] Error parsing near '%PDF-1.2
      %????
      10'

      Where the ? is actually some sort of ? with a question mark inside that I cannot find in the ascii catalog. Please, if anyone has any ideas of how can I make a browser display the open or download dialog from an AJAX postback, let me know. Thanks in advance!

      Kazz


      "Users are there to click on things, not think. Let the archs do the damn thinking."

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      You can't send a response like that via AJAX, the browser is simply not expecting it. Nor do you need to, if you have a link and it does a binary write, your page will not refresh, so the end effect is the same as if you used AJAX.

      Christian Graus Please read this if you don't understand the answer I've given you. If you're still stuck, ask me for more information.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups