ASP. Net AJAX, Response.Write, and PDF
-
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 thatMemoryStream
(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 likeResponse.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."
-
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 thatMemoryStream
(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 likeResponse.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."
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.