rossmcm
rossmcm

Reputation: 5620

Open a new tab in a browser with the response to an ASP request

It's a bit complicated this one... Lets say I have a listing of PDF files displayed in the user's browser. Each filename is a link pointing not to the file, but to an ASP page, say

<--a href="viewfile.asp?file=somefile.pdf">somefile.pdf</a>  

I want viewfile.asp to fetch the file (I've done that bit OK) but I then want the file to be loaded by the browser as if the user had opened the PDF file directly. And I want it to open in a new tab or browser window.

here's (simplified) viewfile.asp:

<%
var FileID   = Request.querystring ("file") ;
var ResponseBody = MyGETRequest (SomeURL + FileID) ;

if (MyHTTPResult == 200)
    { 
    if (ExtractFileExt (FileID).toLowerCase = "pdf")
        {
        ??????  // return file contents in new browser tab
        }        
....
%>

Upvotes: 0

Views: 2448

Answers (2)

Daniel A. White
Daniel A. White

Reputation: 190905

I would do this.

<a href="viewfile.asp?file=somefile.pdf" target="_blank">somefile.pdf</a>

That way this opens in a new window/tab. Any server side language does not have control of the browser.

To serve it as a PDF, call

<% response.ContentType="application/pdf" %>

Upvotes: 1

Marcus Leon
Marcus Leon

Reputation: 56669

As Daniel points out you can control whether to open in a new window but not a new tab. If the user has configured their browser so that new windows should open in new tabs (like I do) then you're golden. If not it will open in a new window. You can't control tabs.

Upvotes: 2

Related Questions