views:

1042

answers:

2

Hi All, I want to Stream a PDF to a new browser instance. I currently have this

Response.ContentType = "application/pdf"
Response.AddHeader("Content-Disposition", "inline; filename=""" & Path.GetFileName(pdfFile) & """")
Response.AddHeader("Content-Length", stream.Length)
Response.BinaryWrite(stream.ToArray())
Response.Flush()

But this takes over my existing window and navigates me away from the page. I want to spawn a new tab or window and display the PDF there.

Does anyone know how to do this?

+2  A: 

You might want to consider the <a> target attribute. You can use this to open the PDF in a new Window, perhaps using something like:

<a href="GeneratePdf.ashx?somekey=10" target="_blank">

Update- as you have now said that you don't mind whether it is in a browser window or not, my preferred technique is to change the content disposition to attachment as per MercerTraieste answer.

If you are using ASP.NET, it is definitely a good idea to consider writing a custom HttpHandler to stream the PDF for you.

RichardOD
+1  A: 

You could force a download, this kinda solves youur problem:

Response.AddHeader("Content-Disposition", "attachment;filename""" & Path.GetFileName(pdfFile) & """")
Mercer Traieste
That's how I normally send PDFs to the client. Though technically it will open directly in Acrobat if you the open option is selected, which isn't a new browser window.However it is still my preferred solution so +1.
RichardOD
see my comments to musicfreak. A new requirement is to not first save the PDF.
Hcabnettek
I actually think this is fine. It didn't need to really open in a browser. It just needed to be opened and readable on the client without navigating them away from the current page.
Hcabnettek
I'm glad that this helps :)
Mercer Traieste