How to open PDF file in a new tab or window instead of downloading it (using asp.net)? How to open PDF file in a new tab or window instead of downloading it (using asp.net)? asp.net asp.net

How to open PDF file in a new tab or window instead of downloading it (using asp.net)?


Response.ContentType = contentType;HttpContext.Current.Response.AddHeader("Content-Disposition", "inline; filename=" + fileName);Response.BinaryWrite(fileContent);

And

<asp:LinkButton OnClientClick="openInNewTab();" OnClick="CodeBehindMethod".../>

In javaScript:

<script type="text/javascript">    function openInNewTab() {        window.document.forms[0].target = '_blank';         setTimeout(function () { window.document.forms[0].target = ''; }, 0);    }</script>

Take care to reset target, otherwise all other calls like Response.Redirect will open in a new tab, which might be not what you want.


Instead of loading a stream into a byte array and writing it to the response stream, you should have a look at HttpResponse.TransmitFile

Response.ContentType = "Application/pdf";Response.TransmitFile(pathtofile);

If you want the PDF to open in a new window you would have to open the downloading page in a new window, for example like this:

<a href="viewpdf.aspx" target="_blank">View PDF</a>


this may help

Response.Write("<script>");Response.Write("window.open('../Inventory/pages/printableads.pdf', '_newtab');");Response.Write("</script>");