How to open pdf files in browser

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

mannevenuAsked:
Who is Participating?
 
bmatumburaConnect With a Mentor Commented:
Indeed, if it works on your localhost it should work on the internet/intranet as well.
0
 
bmatumburaCommented:
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
 
abelCommented:
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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

 
abelCommented:
> returns an actual Windows file path instead.

ah, good point too ;-)
0
 
abelCommented:
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
 
bmatumburaCommented:
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
 
mannevenuAuthor Commented:
Yeah thanx for the help
but will this code work over the internet
this is working fine for Localhost
0
 
abelCommented:
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
 
abelCommented:
> 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
 
mannevenuAuthor Commented:
okies bmatumbura thanx for the help
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.