3

I am using VB.NET to write a pdf file directly to the browser, which opens it right away replacing the content in a current window. I am trying to do is set a parameter which will cause binarywrite in a new tab / window.

' Set the appropriate ContentType.
Response.ContentType = "Application/pdf"

' Write file directly to browser.
Response.BinaryWrite(binaryData)
Response.End()

There is has to be something that I can set which will cause this to write binary PDF in a new window. Like Response.Target = "_blank" ?????

Craig W.
  • 17,838
  • 6
  • 49
  • 82
  • 1
    "target" is defined client-side in the "a" tag. It would be a serious security caveat if you could target a client window from a server-side command. – tgolisch Jul 02 '14 at 17:19
  • ""target" is defined client-side in the "a" tag. It would be a serious security caveat if you could target a client window from a server-side command." I don't see how that is because I have the option to stream to PDF file ( I can do that ), or the browser window ( I can do that ), I am sure there is an option for ContentType to set it to try to open in a new tab. –  Jul 02 '14 at 18:14

3 Answers3

6

First, create an additional page named "PDF.aspx" or whatever you would like it called.

Second, in your page, store your "binarydata" variable in a session.

Session("binaryData") = binarydata

And tell it to go to your new page.

Response.Redirect("PDF.aspx")

Third, go to your PDF.aspx code behind page and put the following:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles       Me.Load
    Try
         Response.ContentType = "Application/pdf"
         Response.BinaryWrite(Session("binaryData"))
         Response.End()
    Catch ex As Exception
        Throw ex
    End Try
End Sub

That should produce the result you want. :)

Jack
  • 1,453
  • 1
  • 15
  • 35
  • 1
    I thought there would be an easier way to do this but I guess that is the only way. Thank you. –  Jul 02 '14 at 18:18
  • 3
    @MikeR. PDF.aspx should likely be a generic handler (PDF.ashx), as you don't need the overhead of an entire page. – mason Jul 02 '14 at 19:37
  • 1
    @WyattTroutman No problem. Generic handlers are very useful for obtaining documents or images or other binary files from a database. You can even use them to serve static (regular files) and use them for authorization or download statistics etc. One of the most common uses I see of generic handlers is retrieving images from a database for display in a GridView, with the image id being passed via query string. – mason Jul 02 '14 at 20:09
2

I know this is old but here's what I had done to achieve the OP was after. (Forgive me as this is C# but it shouldn't be too hard to convert it)

This is the original code that I had used that displayed the same behavior that you have described!

Response.ClearContent();
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition", "inline; filename=" + docName);
Response.AddHeader("Content-Length", byteArray.Length.ToString());
Response.BinaryWrite(byteArray);
Response.End();

There are two key changes that I had to make to get this to work the way I wanted (Download the file as PDF, then open in a new tab when the user had clicked on that attachment.) and they are both on line 3 of the code above.

Change this

Response.AddHeader("Content-Disposition", "inline; filename=" + docName);

To this

Response.AddHeader("Content-Disposition", "attachment; filename=" + docName + ".pdf");

If you specify the Content-Disposition as "Inline" it will output the file content in your current window.

You need to change that disposition to "attachment" AND make sure your extension suffix is explicitly set to .pdf.

Bonez024
  • 1,365
  • 1
  • 13
  • 21
1

How about an alternative? Write to the browser some client side script to open a new window/tab.

ClientScript.RegisterStartupScript(this.GetType(), "scriptkey", "window.open('MyPDFDisplayer.ashx');");

Then create a new generic handler MyPDFDisplayer.ashx that writes the PDF to the window.

context.Response.ContentType = "application/pdf";
context.Response.BinaryWrite(binaryData);
context.Response.End();

I'm a C# guy, so you'll have to translate but it should be pretty easy.

Community
  • 1
  • 1
mason
  • 31,774
  • 10
  • 77
  • 121