ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2017 www.aspforums.com. All rights reserved.how to open pdf file in browser in new tabHi @manishpand,<br />Please try the following<br /><br /><a href = 'link(Threads/186080/Open-PDF-File-in-New-Window-or-New-Tab-on-Button-click-in-ASPNet/)'>Open PDF File in New Window or New Tab on Button click in ASP.Net</a><br /><br />It might help you.<br /><br />Cheers Andrea.http://www.aspforums.net/Threads/566741/how-to-open-pdf-file-in-browser-in-new-tab/http://www.aspforums.net/Threads/566741/how-to-open-pdf-file-in-browser-in-new-tab/Thu, 01 Sep 2016 13:19:28 GMThow to open pdf file in browser in new tab<p><a class="username" rel="manishpand">Hi manishpand</a>,</p> <p>I have created sample code which fullfill requirement</p> <p><span style="text-decoration: underline;">HTML</span></p> <pre class="brush: html"> &lt;asp:GridView ID=&#34;GridView1&#34; runat=&#34;server&#34; AutoGenerateColumns=&#34;false&#34;&gt; &lt;Columns&gt; &lt;asp:BoundField DataField=&#34;Name&#34; HeaderText=&#34;File Name&#34; /&gt; &lt;asp:TemplateField ItemStyle-HorizontalAlign=&#34;Center&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:LinkButton ID=&#34;lnkView&#34; runat=&#34;server&#34; Text=&#34;View&#34; OnClick=&#34;View&#34; CommandArgument=&#39;&lt;%# Eval(&#34;Id&#34;) %&gt;&#39;&gt;&lt;/asp:LinkButton&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;/Columns&gt; &lt;/asp:GridView&gt;</pre> <p><span style="text-decoration: underline;">C#</span></p> <pre class="brush: csharp"> private void BindGrid() { string constr = ConfigurationManager.ConnectionStrings[&#34;constr&#34;].ConnectionString; using (SqlConnection con = new SqlConnection(constr)) { using (SqlCommand cmd = new SqlCommand()) { cmd.CommandText = &#34;SELECT Id, Name FROM tblFiles&#34;; cmd.Connection = con; con.Open(); GridView1.DataSource = cmd.ExecuteReader(); GridView1.DataBind(); con.Close(); } } } protected void View(object sender, EventArgs e) { string url = string.Format(&#34;Default.aspx?Id={0}&#34;, (sender as LinkButton).CommandArgument); string script = &#34;&lt;script type=&#39;text/javascript&#39;&gt;window.open(&#39;&#34; + url + &#34;&#39;)&lt;/script&gt;&#34;; this.ClientScript.RegisterStartupScript(this.GetType(), &#34;script&#34;, script); }</pre> <p>Second Page:</p> <p><span style="text-decoration: underline;">HTML</span></p> <pre class="brush: html"> &lt;div&gt; &lt;asp:Literal ID=&#34;ltEmbed&#34; runat=&#34;server&#34; /&gt; &lt;/div&gt;</pre> <p><span style="text-decoration: underline;">C#</span></p> <pre class="brush: csharp">protected void Page_Load(object sender, EventArgs e) { string embed = &#34;&lt;object data=\&#34;{0}{1}\&#34; type=\&#34;application/pdf\&#34; width=\&#34;500px\&#34; height=\&#34;300px\&#34;&gt;&#34;; embed += &#34;If you are unable to view file, you can download from &lt;a href = \&#34;{0}{1}&amp;download=1\&#34;&gt;here&lt;/a&gt;&#34;; embed += &#34; or download &lt;a target = \&#34;_blank\&#34; href = \&#34;http://get.adobe.com/reader/\&#34;&gt;Adobe PDF Reader&lt;/a&gt; to view the file.&#34;; embed += &#34;&lt;/object&gt;&#34;; ltEmbed.Text = string.Format(embed, ResolveUrl(&#34;~/FileCS.ashx?Id=&#34;), Request.QueryString[&#34;Id&#34;]); }</pre> <p><span style="text-decoration: underline;">Add Handler</span> </p> <pre class="brush: csharp">public void ProcessRequest(HttpContext context) { int id = int.Parse(context.Request.QueryString[&#34;Id&#34;]); byte[] bytes; string fileName, contentType; string constr = ConfigurationManager.ConnectionStrings[&#34;constr&#34;].ConnectionString; using (SqlConnection con = new SqlConnection(constr)) { using (SqlCommand cmd = new SqlCommand()) { cmd.CommandText = &#34;SELECT Name, Data, ContentType FROM tblFiles WHERE Id=@Id&#34;; cmd.Parameters.AddWithValue(&#34;@Id&#34;, id); cmd.Connection = con; con.Open(); using (SqlDataReader sdr = cmd.ExecuteReader()) { sdr.Read(); bytes = (byte[])sdr[&#34;Data&#34;]; contentType = sdr[&#34;ContentType&#34;].ToString(); fileName = sdr[&#34;Name&#34;].ToString(); } con.Close(); } } context.Response.Buffer = true; context.Response.Charset = &#34;&#34;; if (context.Request.QueryString[&#34;download&#34;] == &#34;1&#34;) { context.Response.AppendHeader(&#34;Content-Disposition&#34;, &#34;attachment; filename=&#34; + fileName); } context.Response.Cache.SetCacheability(HttpCacheability.NoCache); context.Response.ContentType = &#34;application/pdf&#34;; context.Response.BinaryWrite(bytes); context.Response.Flush(); context.Response.End(); } public bool IsReusable { get { return false; } }</pre> <p>I hope help you.</p>http://www.aspforums.net/Threads/566741/how-to-open-pdf-file-in-browser-in-new-tab/http://www.aspforums.net/Threads/566741/how-to-open-pdf-file-in-browser-in-new-tab/Fri, 02 Sep 2016 03:30:05 GMT