Opening PDF in new tab using ASP.NET

Asked

Viewed 587 times

1

I am testing the code below, but there is no action when I click button to open a PDF in new tab.

You’re missing something below?

Test.aspx

Clique <asp:LinkButton OnClick="linkPDF_click" ID="linkPDF" runat="server">aqui</asp:LinkButton> para abrir seu PDF.

Teste.aspx.Cs

protected void linkPDF_click(object sender, EventArgs e) 
{
  string FilePath = Server.MapPath("~/MediaFiles/Comuns/PDF/Arquivo Fatura.pdf");
  WebClient User = new WebClient();
  Byte[] FileBuffer = User.DownloadData(FilePath);
  if(FileBuffer != null)
  {
    Response.ContentType = "application/pdf";
    Response.AddHeader("content-length", FileBuffer.Length.ToString());
    Response.BinaryWrite(FileBuffer);
  }
}
  • but what the request returns, gives some error?

1 answer

1


friend, I made one that did something similar that worked as follows:

vc would put in the case the direct code in HTML, in the view that would have the link to click on the PDF

<strong>
   <a href="~/MediaFiles/Comuns/PDF/Arquivo Fatura.pdf" target="_blank">Visualizar PDF do manual</a>
</strong>

I tested here in mine this working perfectly

  • Good, it worked, buddy! Thank you very much!

  • I’m happy to help :D

Browser other questions tagged

You are not signed in. Login or sign up in order to post.