Click here to Skip to main content
15,888,454 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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


C#
%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)

   <pre lang="c#"> function pdf_report() {
        $.ajax({
            type:"POST",
            url: "Handlers/pdf_Handler.ashx",
            contentType: "application/json; charset=utf-8",
            success: OnComplete,
            error: OnFail
        });
    }</pre>

//this is how my handler looks like

<pre lang="c#">
 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=Report.pdf");
        HttpContext.Current.Response.End();
        HttpContext.Current.Response.Flush();
        HttpContext.Current.Response.Close();
    }
    }
}

</pre>

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.
Posted
Updated 1-Nov-12 1:28am
v2

1 solution

Hello ajithk444,

If you simply want to trigger downloading of the file then change your function pdf_report() to
JavaScript
function pdf_report() {
    window.location = "Handlers/pdf_Handler.ashx",            
}


To display it on the page I would use iframe. Place this to your html:
HTML
<iframe id="pdfWrapper" src="" width="100%"></iframe>


And when you want to display it on the page call this function:
JavaScript
function display report() {
   $("#pdfWrapper").attr("src", "Handlers/pdf_Handler.ashx");
}


I also guess that the web page if not in the same directory as the handler. If this is the case specify the full path - e.g. /Handlers/pdf_Handler.ashx.

Let me know if this worked for you.
 
Share this answer
 
Comments
ajithk444 2-Nov-12 0:54am    
Hi,
I need to open in a new window.Can u help me other than iframe. I jus need the browser to prompt for downloading the pdf generated y the httphandler.
In my code the pdf is getting generated correctly. i am jus missing something to handle that pdf from handler and bring it to browser for download or save. this is my prblem. Can u pls help me ..
Jan Mikeska 2-Nov-12 4:41am    
Hi, did the downloading worked when you used the modified pdf_report function?

To display the pdf in a new window create a new web page, place the <iframe id="pdfWrapper" src="Handlers/pdf_Handler.ashx" width="100%"></iframe> into it, and when you want to open the page from javascript use window.open('http://myDomain.com/myPdfPage', '_blank').

If you have an issue with the iframe there are some alternatives. You can use object element, embed element or a plugin. Check this out for further info: http://stackoverflow.com/questions/291813/recommended-way-to-embed-pdf-in-html

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900