0
votes

I am trying to open a PDF in a new tab in the browser. So far I have been able to get the PDF in the browser within the "same" page. However I would now like it to open it in a new tab. However I am stuck on how to achieve this?

            Response.Clear()
            Response.ClearContent()
            Response.ClearHeaders()
            Response.ContentType = "application/pdf"
            Response.AddHeader("Content-Disposition", "inline;filename=" + strFileId & fileExtension)
            Response.AddHeader("Content-Length", bytesReturned.Length.ToString())
            Response.OutputStream.Write(bytesReturned, 0, bytesReturned.Length)
            Response.Flush()
            Response.End()

Result: enter image description here

The result is displayed in a gridview, I would like to link each "view" button to a target"_blank" attribute. The "ID" corresponds to the file name.

From my research, I can see you can add a button in the aspx page and give it target "_blank" attribute but I am not sure how to link it to my displayPDF() method?

If the question is unclear, please let me know.

1
You have written this code just to open the file ? - Mairaj Ahmad
Put that code in another asp page, and call it from JS with the _blank attribute. - Caveman
<a href="/displaypdf.asp" target="_blank">VIew Pdf</a>? - Jimmy Chandra
The bytes are being returned from a database, the file name is displayed in a gridview. A view button is clicked to display the pdf current process... - Harry
@JimmyChandra how do I set this for every file displayed in a gridview? - Harry

1 Answers

0
votes

One way is to make view button an anchor and set its attribute to _blank and second is to open file by using code behind.

Call this code on button click

ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "a", "window.open('"+strFileId & fileExtension+"','_blank');", true);