Open a PDF in new tab from URL from my MVC Controller [EDITED]

2.3k Views Asked by At

In my view, I generate a link with a username, acct number and date that is passed to the controller:

   @Html.ActionLink(AccountGroup.AcctNum + " " + AccountGroup.DocDate.ToShortDateString(), "GetIndividualStatement", "Statements",  new { statementDate = @AccountGroup.DocDate.ToShortDateString(), Userid = @ViewBag.UserID, acctNumber = AccountGroup.AcctNum.ToString() }, new { @class = "form-control, col-sm-6,  medwidth" , target = "_blank" })

I have tried to use an AJAX link also:

 @Ajax.ActionLink(AccountGroup.AcctNum + " " + AccountGroup.DocDate.ToShortDateString(), "GetIndividualStatement", "Statements", new { statementDate = @AccountGroup.DocDate.ToShortDateString(), Userid = @ViewBag.UserID, acctNumber = AccountGroup.AcctNum.ToString() }, new AjaxOptions { HttpMethod ="POST", UpdateTargetId = "detailsDiv" }, new { @class = "form-control, col-sm-6,  medwidth" })

Then my controller takes that info generates an encrypted URL for the PDF (I have to create this on the spot in my controller, so I cannot just have a link in my view for the user to click (that would be too easy!). I need to open the URL which returns a PDF. I need to take that and open that PDF in a new tab (while keeping the original tab open). Here is my latest attempt, which opens in the same window, but how do I open in a new tab ?

    WebClient client = new WebClient();
    Byte[] buffer = client.DownloadData(path);
    if (buffer != null)
    {
        Response.ContentType = "application/pdf";
        Response.AddHeader("content-length", buffer.Length.ToString());
        Response.BinaryWrite(buffer);
    }

Thanks to all!

1

There are 1 best solutions below

0
On BEST ANSWER

What you are looking for in the final HTML that you produce is something of the form below, where the link has the target attribute set to '_blank',

<a href="the_pdf_link/" target="_blank">Open PDF</a> 

Get that right and you will get the behaviour you want.

If the issue is in the server-side code then when you solve it, edit your question to include the fix just so anyone else looking in from the future can see both sides of the story.