Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

This is the code for downloading the file.

System.IO.FileStream fs = new System.IO.FileStream(Path+"\\"+fileName, System.IO.FileMode.Open, System.IO.FileAccess.Read);
byte[] ar = new byte[(int)fs.Length];
fs.Read(ar, 0, (int)fs.Length);
fs.Close();

Response.AddHeader("content-disposition", "attachment;filename=" + AccNo+".pdf");
Response.ContentType = "application/octectstream";
Response.BinaryWrite(ar);
Response.End();

When this code is executed, it will ask user to open or save the file. Instead of this I need to open a new tab or window and display the file. How can I achieve this?

NOTE:

File won't necessary be located in the website folder. It might be located in an other folder.

like image 289
smilu Avatar asked Nov 28 '11 09:11

smilu


People also ask

How do I get a PDF to open in a new tab instead of downloading it?

At the top right, click More Settings. At the bottom, click Show advanced settings. Under “Privacy”, click Content settings. Under “PDF Documents," check the box next to "Open PDF files in the default PDF viewer application.” (Uncheck this box if you want PDFs to open automatically when you click them.)

How do I view files in browser instead of downloading?

Click on "Settings" and you'll see a new page pop up in your Chrome browser window. Scroll down to Advanced Settings, click Downloads, and clear your Auto Open options. Next time you download an item, it will be saved instead of opened automatically.

How do I get PDFs to open in browser tab?

Navigate to the "Open With" option and choose "Chrome PDF Viewer" from the drop-down menu. You can also drag a PDF document directly into the browser, and it will open. Using this above outline method, opening a PDF document becomes easy. You can view a downloaded document directly using this method.


4 Answers

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.

like image 143
Nina Avatar answered Oct 08 '22 21:10

Nina


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>
like image 45
janzi Avatar answered Oct 08 '22 23:10

janzi


this may help

Response.Write("<script>");
Response.Write("window.open('../Inventory/pages/printableads.pdf', '_newtab');");
Response.Write("</script>");
like image 2
Karthik Avatar answered Oct 08 '22 22:10

Karthik


You have to create either another page or generic handler with the code to generate your pdf. Then that event gets triggered and the person is redirected to that page.

like image 1
Yetimandaddy Avatar answered Oct 08 '22 21:10

Yetimandaddy