3

I'm looking for a way to stream a PDF file from my server to the browser using .NET 2.0 (in binary).

I'm trying to grab an existing PDF file from a server path and push that up as binary to the browser.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Toran Billups
  • 27,111
  • 40
  • 155
  • 268
  • How is this different than just dropping the PDF on the server and having it served via URL? Is this a dynamically generated PDF? – Eric Schoonover Sep 17 '08 at 16:26
  • @spoon16, there is some security issues with this approach, as any one can see the pdf url from page's viewsource. Since these are static files(.pdf,.htm...etc) they will serve directly by the IIS, to whoever requesting this URL, without checking whether they are logged in or not. – Sunil Sep 20 '12 at 05:52

4 Answers4

5

Here you go: How To Write Binary Files to the Browser Using ASP.NET and Visual C# .NET

jop
  • 82,837
  • 10
  • 55
  • 52
3
  1. Set Content-Type: Response.ContentType = "application/pdf"
  2. Set ContentDisposition, if you want to give a new name for the file: Response.Headers.Add("Content-Disposition", "attachment: filename=file.pdf");
  3. Write the content, using Response.OutputStream as Mr. Kopp said.

Step 2 is not strictly necessary, but it's probably a good idea if you don't want the browser to try to save the PDF with the same name as your ASPX file.

miguel
  • 73
  • 1
  • 2
  • 7
foxxtrot
  • 11,214
  • 4
  • 27
  • 27
1

Write the binary to the output stream, Response.OutputStream. Then just add the header Content-Disposition header.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Darren Kopp
  • 76,581
  • 9
  • 79
  • 93
0

You can just setup a handler or a page that set's the correct response type and output the pdf to the response output buffer.

Quintin Robinson
  • 81,193
  • 14
  • 123
  • 132