0

I have tried a code to open a pdf file in a web browser. It give to allows me to open the file through pdf reader no in the browser. Almost all the codes i found over the internet are also same as this code. But this code doesn't work as i expected.

Help me to figure out the problem in here. I'm using a link button in the aspx.

Here is my code

aspx code

<asp:LinkButton ID="pdfViewLOP" runat="server" Style="margin-left: 10px" OnClick="pdfViewLOP_Click" >View PDF</asp:LinkButton>      

aspx.cs

Response.Write(string.Format("<script>window.open('{0}','_blank');</script>", "viewPDF.aspx"));

Code of the new page which pdf should be displayed

string name=Session["name"].ToString();
int refNo = Convert.ToInt32(name);
string FilePath = Server.MapPath("~/filesPDF/" + refNo + ".pdf");
WebClient User = new WebClient();
Byte[] buffer = User.DownloadData(FilePath);

if (buffer != null)
{
     Response.Clear();
     Response.ContentType = "application/pdf";
     Response.AddHeader("content-length", buffer.Length.ToString());
     Response.BinaryWrite(buffer);
}
Divyang Desai
  • 7,483
  • 13
  • 50
  • 76
Mike
  • 1,017
  • 4
  • 19
  • 34

1 Answers1

-1

How if you directly create a hyperlink with target _blank and path of file as a href of hyperlink.

So the result hyperlink generated inside the html page is

<a href="link_to_pdf" target="_blank">View PDF</a>
Liam
  • 27,717
  • 28
  • 128
  • 190
Vishal Gupta
  • 132
  • 2
  • 6
  • 1
    Can't you suggest me a way to do it as i have done already? Because this code works but not in mine. I may have done a mistake. But i can't find it – Mike Sep 13 '16 at 10:48
  • Hi mate i don't know asp but i certainly know if your code is generating this html tag it will work perfectly. You will not need to change much code you just need to remove some unnecessary code. – Vishal Gupta Sep 13 '16 at 10:51
  • Please read [How do I format my posts using Markdown or HTML?](http://stackoverflow.com/help/formatting) – Liam Sep 13 '16 at 10:52