Click here to Skip to main content
15,905,322 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
How to Open any file from code behind in new browser tab using ASP.NET with C#?
Posted
Updated 13-Aug-12 18:36pm
v2
Comments
Kenneth Haugland 14-Aug-12 0:31am    
Navigate.Url?
amit_83 14-Aug-12 0:35am    
Thanks for reply.
i want to open file from code behind. any other solution?
Rangga Stephen 14-Aug-14 8:00am    
Imports System.Net

Public Class _Default1
Protected Sub bttnpdf_Click(sender As Object, e As EventArgs)
Dim FilePath As String = Server.MapPath("Sample.pdf")
Dim User As New WebClient()
Dim FileBuffer As [Byte]() = User.DownloadData(FilePath)
If not (FileBuffer is nothing) Then
Response.ContentType = "application/pdf"
Response.AddHeader("content-length", FileBuffer.Length.ToString())
Response.BinaryWrite(FileBuffer)
End If
End Sub
End Class

Think youll have to do somehting like this:

previewlink.NavigateUrl = New Url("someAddres", Url.Absolute)
previewlink.Target = "_blank";

Only files supported by the user will open in this new folder though...
 
Share this answer
 
Comments
Abdul Quader Mamun 14-Aug-12 2:11am    
good & nice Answer!
Hiiii
you can try it

<asp:hyperlink id="HyperLink1" target="_blank" runat="server" xmlns:asp="#unknown">HyperLink

it will open your link in new tab.


Ram Sharma
 
Share this answer
 
Comments
amit_83 14-Aug-12 1:22am    
Thanks for reply. i want to open file from code behind. any other solution?
Kenneth Haugland 14-Aug-12 2:14am    
Thanks :)
Hi,

Although you have not specified what type of file you would like to open,

Here is an Trick to Open PDF file in new browser tab using ASP.NET with C#[^]

And Discussion : Open PDF file in browser[^]

Hope you are looking for this,

Thanks
-Amit Gajjar
 
Share this answer
 
Hi try below....

In ur code behind: (write below code)
C#
string url = "./Default1.aspx?EmployeeUID=" + strEmpUID.ToString();

string winFeatures = "toolbar=no,status=no,menubar=no,location=center,scrollbars=yes,resizable=no,height=650,width=950";

ClientScript.RegisterStartupScript(this.GetType(), "newWindow", string.Format("<script type="text/javascript">var popup=window.open('{0}', 'yourWin', '{1}');  popup.focus();</script>", url, winFeatures));
 
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