0

Example in my vb asp.net application

On postback

// ... do stuff
Response.Clear()
Response.Buffer = True
Response.ContentType = "application/pdf"
Response.OutputStream.Write(fileData, 0, fileData.Length)
Response.End()

I have tried multiple ways and google doesn't offer me to much on my searchers regarding using script document.forms[0].target = "_blank"; and other techniques like creating a separate aspx page and storing the binary in a session then serving it up in the load function.

Thought maybe one of you can be my save n grace, thanks in advance

EDIT: Recently tried This guys solution without success.

Community
  • 1
  • 1
Don Thomas Boyle
  • 3,055
  • 3
  • 32
  • 54
  • What are you trying to achieve? You want the PDF to open in new window? That's PDF Reader setting. – dario Mar 11 '15 at 15:52
  • I'm using 3rd party tools (forcibly) to create this PDF - this has to happen in the postback. I'm trying to read this file once completed into memory and serve it into a new window (tab) - unsuccessfully. As far as i can tell PDF reader settings have nothing to do with this. – Don Thomas Boyle Mar 11 '15 at 15:56
  • I were talking about [this](https://helpdesk.egnyte.com/hc/en-us/articles/201637224-How-Do-I-Open-PDF-Files-in-Adobe-and-Not-Internet-Explorer-). If you really need to open a new tab, the technique described sounds fine to me. If you didn't, have a look [here](http://stackoverflow.com/questions/8294057/how-to-open-pdf-file-in-a-new-tab-or-window-instead-of-downloading-it-using-asp) too. – dario Mar 11 '15 at 16:05
  • I can see where your confused now. I'm not attempting to open this in anything other than a browser - regarding the open in adobe link. And to the 2nd link - it won't work for me as their serving up a PDF file that was previously downloaded to the server and isn't coming from a postback. – Don Thomas Boyle Mar 11 '15 at 16:10
  • Can't you save it on disk? – dario Mar 11 '15 at 16:11

1 Answers1

3

It doesn't need to happen in postback. You can create the PDF and serve it from a generic handler (.ashx). On your ASPX page, you can open a new window with the URL pointing to the .ashx page, passing any necessary parameters via query string. Below is C#, but I think you'll get the idea.

PDFCreator.ashx

<%@ WebHandler Language="C#" Class="Handler" %>

using System;
using System.Web;

public class Handler : IHttpHandler
{
    public void ProcessRequest (HttpContext context)
    {
        context.Response.Clear();
        context.Response.Buffer = true;
        context.Response.ContentType = "application/pdf";
        var fileData = Database.GetFileData(); //this might be where you grab the query string parameters and pass them to your function that returns the PDF stream
        context.Response.OutputStream.Write(fileData, 0, fileData.Length);
        context.Response.End();
    }

    public bool IsReusable
    {
        get {return false;}
    }
}

ASPX page Javascript

window.open("PDFCreator.ashx", "_blank");
//or
window.open('<%= ResolveClientUrl("~/PDFCreator.ashx") %>', '_blank');

If you still want to have it occur after a postback with the generic handler technique, try this (again, C#):

ClientScriptManager.RegisterStartupScript(this.GetType(), "openpdf", "window.open('PDFCreator.ashx', '_blank');", true); //You can also use ResolveClientUrl if necessary
mason
  • 31,774
  • 10
  • 77
  • 121