Click here to Skip to main content
15,891,993 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hai,
I would like to read pdf files in our webapplication
I used generichandler and put the following code.now its opening with save dialog box.but we want to open without asking save Dialog box.
Please help
Thank you,
Soumya

public void ProcessRequest (HttpContext context) {
String sfilepath = context.Server.MapPath("pdf");
string pdffile= sfilepath + "//" + "cat.pdf";
System.IO.FileInfo objFileInfo = new System.IO.FileInfo(exfile);
System.IO.FileStream oStream = null;
String strNewfilename = "Newname_ABC" + objFileInfo.Extension;
if(objFileInfo.Exists)
{
try
{
oStream = objFileInfo.OpenRead();
if (oStream.CanRead)
{
context.Response.Clear();
context.Response.ContentType = "application/octet-stream";
context.Response.AddHeader("Content-Disposition", "inline; filename=\"" + strNewfilename + "\"");
context.Response.WriteFile(pdffile);
oStream.Close();
context.Response.Flush();
context.Response.End();
}
}
}


[Edited]Code is blocked in "pre" tags[/Edited]
Posted
Updated 14-Mar-11 0:08am
v2

Try below code.
I have made some changes in it. You need to render a dynamic HtmlAnchor and place the link of it in Literal rather then placing a open/save dialog box.

In aspx add one Literal control.

<asp:Literal ID="ltrPdfLink" runat="server"></asp:Literal>


Replace your code with below one. You need to modify some parameters in below function as per your requirement like VirtualFilePath.


public void ProcessRequest(HttpContext context)
    {
        string exfile = "";
        String sfilepath = context.Server.MapPath("pdf");
        string pdffile = sfilepath + "//" + "cat.pdf";
        System.IO.FileInfo objFileInfo = new System.IO.FileInfo(exfile);
        System.IO.FileStream oStream = null;
        String strNewfilename = "Newname_ABC" + objFileInfo.Extension;
        string VirtualFilePath = "http://yoursite/yourfolder/";
        if (objFileInfo.Exists)
        {
            ltrPdfLink.Text = "<a href=\\" + VirtualFilePath + pdffile + ">Click here for PDF</a>";
        }
    }


You can place below line to open pdf in new window.

ltrPdfLink.Text = "<a target='_blank' href=\\" + VirtualFilePath + pdffile + ">Click here for PDF</a>";


Hope it helps.
 
Share this answer
 
Hai Thank you for your response


still its asking Save dialogBox


We would like to implement with the help of GenericHandler


Pls help

Thank you,
soumya
 
Share this answer
 
Comments
That's Aragon 14-Mar-11 8:48am    
Could you please post your detail code here ? like on click of button your are going to call the above code routine or any other method your are following. Details will be more helpful to resolve issue. If you are using any handler for pdf file then pl. post its routine as well.

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