Hcabnettek
Hcabnettek

Reputation: 12928

Stream PDF to new browser instance or tab?

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?

Upvotes: 3

Views: 7869

Answers (2)

RichardOD
RichardOD

Reputation: 29157

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.

Upvotes: 3

Mercer Traieste
Mercer Traieste

Reputation: 4678

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

Response.AddHeader("Content-Disposition", "attachment;filename""" & Path.GetFileName(pdfFile) & """")

Upvotes: 3

Related Questions