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
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);
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hello experts friends......
Plz give your suggestion...... I m waiting for that...
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.
ASKER
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
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("Con tent-Dispo sition", "attachment; filename="+ filename);
In the above, the filename variable, should NOT contain the entire path. It should only contain the FileName with extension.
//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("Con
In the above, the filename variable, should NOT contain the entire path. It should only contain the FileName with extension.
good job
Open in new window