Link to home
Start Free TrialLog in
Avatar of ravi24june
ravi24june

asked on

Force open / save pdf file in C# asp.net

HI friends,

 I m new in C#. I am facing a problem.
 
I m using a Grid view to display record from database. In one fileld the pdf file name is coming from database. On clicking of file name the pdf file is opening from the server.
 
 The problem is , pdf fileis directly open in browser or popup block the pdf file.

 I want to open a dialog box which asks for open save cancel for pdf file.

Thanks
protected void DetailView_SelectedIndexChanged(object sender, EventArgs e)
    {
        string filename=DetailView.SelectedRow.Cells[6].Text.ToString();
 
        if (filename == "")
        {
            LblMsg.Text = "File is not Available";
        }
        else
        {
           filename = Request.Url.AbsoluteUri.Substring(0, Request.Url.AbsoluteUri.LastIndexOf("/") + 1) + @"Published/" + filename;
           string popupScript = "<script language='javascript'>" +
                                  " window.open('" + filename + " ','_blank')" +
                                   "</script>";
           Page.RegisterStartupScript("PopupScript", popupScript);
 
        }
        
        
    }

Open in new window

Avatar of CB_Thirumalai
CB_Thirumalai
Flag of India image

Can you try this?
// 1.
// if the file name is coming from the query string use,
// Request.QueryString["FileURL"], where FileURL is one of the
// query string param.
// 2.
// If you know the file location and just wanted to download it, when
// the page is called then comment out the first line and the last line
// of below code.
// If you choose 2, then in the third line, instead of
//    Server.UrlDecode(Request.QueryString["FileURL"].ToString())
// put your file path
if (Request.QueryString["FileURL"] != null)
{
    string sFileURL = Server.UrlDecode(Request.QueryString["FileURL"].ToString());
    Response.Clear();
    if (!System.IO.File.Exists(sFileURL))
    {
        Response.ContentType = "text/HTML";
        Response.Write("<html><head><title>Open/Save File</title><script type='text/javascript' language='javascript'>function CloseMe(){window.open('','_self');self.close();}</script></head><body><h1>The file has been deleted or moved!</h1><br/><input type='button' id='butClose' onclick='CloseMe();' value='Close'/></body></html>");
    }
    else
    {
        Response.ContentType = "application/pdf";
        Response.AppendHeader("Content-Disposition", "attachment; filename="
            + sFileURL.Substring(sFileURL.LastIndexOf("\\") + 1));
        Response.AppendHeader("Pragma", "no-cache");
        Response.TransmitFile(sFileURL);
    }
    Response.End();
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of ravi24june
ravi24june

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ravi24june
ravi24june

ASKER

Hello experts friends......

 Plz give your suggestion......  I m waiting for that...
I think the TransmitFile method needs the physical location on the hard drive rather than a virtual path.  Please use the physical location instead and see.
Thx thirumalai.

This is working fine.

But 1 more problems related this .

1. when file name is 39_1_2008.pdf , then output is fine. but when i used 39_1_2008.pdf#page=4 [directly go to page 4] , then i got file not found exception.
                   In the earlier case 39_1_2008.pdf#page=4, i can directly navigate on page 4.

What will be the solution for direct page navigation in pdf file.

2. The dialog box (open/save/cancel) shows the server hard disk drive name+ folder name.
           I want to display only the filename not drive or folder name.


waiting for your answer....

Thanks
Ravi



1. There is no solution for direct page navigation as you are downloading a file.  If you # at the last, then just check for that and trim it. you can use below.
    //for example you have "case 39_1_2008.pdf#page=4" stored in filename variable as,
    string filename = "case 39_1_2008.pdf#page=4";
    filename = filename.substring(0, filename.IndexOf('#')-1);
2. Response.AppendHeader("Content-Disposition", "attachment; filename="+ filename);
In the above, the filename variable, should NOT contain the entire path.  It should only contain the FileName with extension.
good job