?
Solved

How to open pdf files in browser

Posted on 2009-04-17
10
Medium Priority
?
2,119 Views
Last Modified: 2012-06-27
I have a LinkButton which when clicked should open a pdf file which is stored in the server. It says file could not be opened.
aspx file
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="test.aspx.cs" Inherits="TrainingCenter_test" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton1_Click">LinkButton</asp:LinkButton></div>
    </form>
</body>
</html>
 
 
Code file(C#) .cs file
public partial class TrainingCenter_test : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string path=Server.MapPath("..\\temp\\123.pdf");
        LinkButton1.Attributes.Add("onclick", "window.open('"+path+"')");
    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        
    }
}

Open in new window

0
Comment
Question by:mannevenu
  • 5
  • 3
  • 2
10 Comments
 
LVL 11

Expert Comment

by:bmatumbura
ID: 24165885
You need to return a relative path to the file that can be used when a client requests the file via the HTTP protocol.

The statement:

string path=Server.MapPath("..\\temp\\123.pdf");

returns an actual Windows file path instead.

Thry the code below:
public partial class TrainingCenter_test : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string path="temp\\123.pdf";
        LinkButton1.Attributes.Add("onclick", "window.open('"+path+"')");
    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        
    }
}

Open in new window

0
 
LVL 39

Expert Comment

by:abel
ID: 24165892
If the file could not be opened then either the content type is not correct, or the user browsing to that page does not have Acrobat (or other PDF viewer) installed.
0
 
LVL 39

Expert Comment

by:abel
ID: 24165893
> returns an actual Windows file path instead.

ah, good point too ;-)
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 39

Expert Comment

by:abel
ID: 24165897
Btw: internet URLs work with forward slashes only. If I take the solution of bmatumbura, change it like this to remove the backslashes (but credits to bmatumbura for the solution):

public partial class TrainingCenter_test : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string path="temp/123.pdf";
        LinkButton1.Attributes.Add("onclick", "window.open('"+path+"')");
    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        
    }
}

Open in new window

0
 
LVL 11

Expert Comment

by:bmatumbura
ID: 24165901
My mistake in the last post, the the code should as shown below:

(NOTE: path returned must something like: temp/123.pdf)
public partial class TrainingCenter_test : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string path="temp\/123.pdf";
        LinkButton1.Attributes.Add("onclick", "window.open('"+path+"')");
    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        
    }
}

Open in new window

0
 

Author Comment

by:mannevenu
ID: 24165951
Yeah thanx for the help
but will this code work over the internet
this is working fine for Localhost
0
 
LVL 39

Expert Comment

by:abel
ID: 24165982
if it works on localhost, it is likely to work on internet as well (provided you set up your server, firewall, iis etc correctly).
0
 
LVL 39

Expert Comment

by:abel
ID: 24165992
> string path="temp\/123.pdf";

this will return an error and will not compile. A forward slash does not need to be escaped. Remove the backslash:

  string path="temp/123.pdf";

0
 
LVL 11

Accepted Solution

by:
bmatumbura earned 1500 total points
ID: 24165995
Indeed, if it works on your localhost it should work on the internet/intranet as well.
0
 

Author Comment

by:mannevenu
ID: 24166094
okies bmatumbura thanx for the help
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Screencast - Getting to Know the Pipeline
Suggested Courses
Course of the Month14 days, 11 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question