I have an application in which i have inplemented an http handler. I m calling this handler with jquery from my aspx button click. I have written code for the handler to return a pdf file. I want the browser to prompt for open or save option when i click
the button. My code is rendering something like this
%PDF-1.3 3 0 obj <> endobj 4 0 obj <> stream ...
this is my code for calling the http handler where i have the method for creating a pdf(using active reports)
the browser is not prompting for opening/saving the file. I dono whether i have handled the output from handler correctly or not. In the "OnSuccess" function i jus printed the output from my handler. the alert is showing the code like this.
%PDF-1.3 3 0 obj <> endobj 4 0 obj <> stream ...
this is my full jQuery code.
function pdf_report() {
$.ajax({
type:"POST",
url: "Handlers/pdf_Handler.ashx",
contentType: "application/json; charset=utf-8",
success: OnComplete,
error: OnFail
});
}
function OnComplete(result) {
alert(result);
}
function OnFail(result) {
alert('Request failed');
}
how can i make my browser prompt for open or save this pdf.?
For me one problem is that you are specifying json as the content type and that's why you get plain text of the "binary" content of the PDF document as result. I cannot figure out why are you making an ajax request to open the PDF file as a simple anchor
would do the trick. So for me you have these options:
In this post custom HTML is passed and from an aspx page the excel is rendered. In your case you need to render a PDF. Let us know in case of any issue.
Thanks & Regards
Anup Das Gupta
Mark as Answer if you feel so. Visit My Blog
I checked it. But am not able to get through it. Am just a beginner in JQuery so i dont have much idea about it. Pls can u elaborae and give a solution n my scenario. hope you understood my scenario.?pls do guide me..
Everything is working fine in my handler. I jus need to handle the output from my handle in jquery i think so this is my problem. pls check my code and give feedback
ajithkumar90
Member
1 Points
24 Posts
Render pdf to open or save in client side using JQuery call to a HttpHandler
Nov 01, 2012 09:03 AM|LINK
hi,
I have an application in which i have inplemented an http handler. I m calling this handler with jquery from my aspx button click. I have written code for the handler to return a pdf file. I want the browser to prompt for open or save option when i click the button. My code is rendering something like this
this is my code for calling the http handler where i have the method for creating a pdf(using active reports)
function pdf_report() { $.ajax({ type:"POST", url: "Handlers/pdf_Handler.ashx", contentType: "application/json; charset=utf-8", success: OnComplete, error: OnFail }); }this is how my handler looks like
public void ProcessRequest (HttpContext context) { PDFExport(); } public bool IsReusable { get { return false; } } public void PDFExport() { MemoryStream m_stream = new MemoryStream(); //my code logic for creating a pdf. pdfExport1.Export(report.Document, m_stream); HttpContext.Current.Response.BinaryWrite(m_stream.ToArray()); HttpContext.Current.Response.ContentType = "application/pdf"; HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=BWA_YachtReport.pdf"); HttpContext.Current.Response.End(); HttpContext.Current.Response.Flush(); HttpContext.Current.Response.Close(); } } }please help me how to open this pdf file in client side in my browser when i click html control button in my asp page.
Neodynamic
Participant
1160 Points
316 Posts
Re: Render pdf to open or save in client side using JQuery call to a HttpHandler
Nov 01, 2012 10:23 AM|LINK
What is the issue at all? I mean, what are you getting there? an error? browser does not prompt for opening/saving the file? Elaborate bit please.
ajithkumar90
Member
1 Points
24 Posts
Re: Render pdf to open or save in client side using JQuery call to a HttpHandler
Nov 01, 2012 11:38 AM|LINK
the browser is not prompting for opening/saving the file. I dono whether i have handled the output from handler correctly or not. In the "OnSuccess" function i jus printed the output from my handler. the alert is showing the code like this.
this is my full jQuery code.
function pdf_report() { $.ajax({ type:"POST", url: "Handlers/pdf_Handler.ashx", contentType: "application/json; charset=utf-8", success: OnComplete, error: OnFail }); } function OnComplete(result) { alert(result); } function OnFail(result) { alert('Request failed'); }how can i make my browser prompt for open or save this pdf.?
Neodynamic
Participant
1160 Points
316 Posts
Re: Render pdf to open or save in client side using JQuery call to a HttpHandler
Nov 01, 2012 02:05 PM|LINK
For me one problem is that you are specifying json as the content type and that's why you get plain text of the "binary" content of the PDF document as result. I cannot figure out why are you making an ajax request to open the PDF file as a simple anchor would do the trick. So for me you have these options:
#1 Just replace your code with a simple anchor:
<a href="handlers/pdf_handler.ashx">PDF Report</a>
#2 Or try changing the contentType to "application/octet-stream" instead of "application/json; charset=utf-8"
asteranup
All-Star
30184 Points
4906 Posts
Re: Render pdf to open or save in client side using JQuery call to a HttpHandler
Nov 01, 2012 03:05 PM|LINK
Hi,
This is not possible with ajax call. There is another way you can implement this. Using a temporary ddynamic form submit. Check the following post-
http://growingtech.blogspot.in/2012/10/export-html-to-excel-using-jquery-and.html
In this post custom HTML is passed and from an aspx page the excel is rendered. In your case you need to render a PDF. Let us know in case of any issue.
Anup Das Gupta
Mark as Answer if you feel so. Visit My Blog
ajithkumar90
Member
1 Points
24 Posts
Re: Render pdf to open or save in client side using JQuery call to a HttpHandler
Nov 02, 2012 04:41 AM|LINK
Hi,
it is still the same. I am using an image. when i click that image it is calling my code
$("#btnPrintRep").click(function (e) { var str=""; // some data $.ajax({ type: 'POST', url: '../HttpHandlers/PdfHandler.ashx', data: str, async: false, contentType: "application/octet-stream", success: function (respose) { alert(respose); }, error: function (result) { alert(JSON.stringify(result)); } }); });Still I am getting an error code.like
asteranup
All-Star
30184 Points
4906 Posts
Re: Render pdf to open or save in client side using JQuery call to a HttpHandler
Nov 02, 2012 05:11 AM|LINK
Hi,
Have you checked my post?
Anup Das Gupta
Mark as Answer if you feel so. Visit My Blog
ajithkumar90
Member
1 Points
24 Posts
Re: Render pdf to open or save in client side using JQuery call to a HttpHandler
Nov 02, 2012 05:34 AM|LINK
Hi,
I checked it. But am not able to get through it. Am just a beginner in JQuery so i dont have much idea about it. Pls can u elaborae and give a solution n my scenario. hope you understood my scenario.?pls do guide me..
ajithkumar90
Member
1 Points
24 Posts
Re: Render pdf to open or save in client side using JQuery call to a HttpHandler
Nov 02, 2012 05:36 AM|LINK
Everything is working fine in my handler. I jus need to handle the output from my handle in jquery i think so this is my problem. pls check my code and give feedback
Neodynamic
Participant
1160 Points
316 Posts
Re: Render pdf to open or save in client side using JQuery call to a HttpHandler
Nov 02, 2012 10:05 AM|LINK
Again, for me a simple anchor should do the trick. If you have an image then wrap it with an anchor like this:
You do not need to perform such ajax call. try it.