Click here to Skip to main content
15,897,704 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a LinkButton in gridview.
I want to open pdf file from database in new tab.
In my code pdf is opening in same window not in new tab.

What I have tried:

ASP.NET
<asp:TemplateField HeaderText="View File">
<ItemTemplate>
<asp:LinkButton ID="lbtnViewFile" runat="server"  CommandArgument='<%#System.Web.HttpUtility.HtmlEncode(Eval("AppCode") + "," + Eval("AttachmentCode") + "," + Eval("AttachmentCodeSerialNumber"))%>'                                                                    OnCommand="lbtnViewFile_Click">View</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>




C#
protected void lbtnViewFile_Click(object sender, CommandEventArgs e)
  {
     LinkButton btn = (LinkButton)sender;
     string[] CommandArgument = btn.CommandArgument.Split(',');
     long lng_AppCode = Convert.ToInt32(CommandArgument[0]);
     int int_attachmentCode = Convert.ToInt32(CommandArgument[1]);
     int int_attachmentCodeSerialNumber = Convert.ToInt32(CommandArgument[2]);
    DownloadFiles(lng_AppCode,
                         int_attachmentCode, int_attachmentCodeSerialNumber);

  }



C#
Private  int DownloadFiles(long lng_AppCode, int int_attachmentCode, int int_attachmentCodeSerialNumber)   
{
	try
        {
        	int intResult = 0;
            ABC obj_ABC = new ABC();
            A obj_A = 
              obj_ABC.DownloadFile(lng_AppCode, 
              int_attachmentCode, int_attachmentCodeSerialNumber);

            if (obj_A != null)
            {
                byte[] bytes = obj_A.AttachmentFile;
                HttpContext.Current.Response.Buffer = true;
                HttpContext.Current.Response.Charset = "";
                HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
                HttpContext.Current.Response.ContentType = obj_A.ContentType;

                HttpContext.Current.Response.BinaryWrite(bytes);
                if (obj_A.ContentType == "application/pdf")
                {
                    HttpContext.Current.Response.AddHeader("content-disposition", "inline;filename=" + "file_" + Convert.ToString(lng_AppCode) + "_" + Convert.ToString(int_attachmentCode) + "_" + Convert.ToString(int_attachmentCodeSerialNumber) + obj_A.FileExtension);
                  
                   

                  
                }
                else
                    HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + "file_" + Convert.ToString(lng_AppCode) + "_" + Convert.ToString(int_attachmentCode) + "_" + Convert.ToString(int_attachmentCodeSerialNumber) + obj_A.FileExtension);

                HttpContext.Current.Response.Flush();

                intResult = 1;
            }
            return intResult;
        }
        catch (Exception)
        {
            return 0;
        }

    }
Posted
Updated 24-Apr-19 20:14pm

1 solution

Google is your friend: Be nice and visit him often. He can answer questions a lot more quickly than posting them here...

A very quick search using your subject as most of the search term gave nearly a million hits, including here and SO with code: open pdf file from database in new tab C# - Google Search[^]

In future, please try to do at least basic research yourself, and not waste your time or ours.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900