user2315877
user2315877

Reputation: 103

Best way to pass a stream pdf to HttpHandler at asp.net?

I have an aspx page with a link button and I want to redirect when click to a httpHandler with the stream of the file I want to show. For example: The link shows a reference to my handler, at the onclick event of link button I get the stream and pass it someway to the context, then at the ProcessRequest I show the stream. How could I do pass the stream to my context or what is the best way? Thanks!

Upvotes: 3

Views: 4138

Answers (1)

Oskar Lindberg
Oskar Lindberg

Reputation: 2294

It has been mentioned before that since the response is exposed as a Stream through the HttpContext.Response.OutputStream, if your source is also a Stream you can simply copy one stream to the other: Write PDF stream to response stream.

Several answers in that thread are applicable to ASP.NET in general, and the "standard approach" is among them, with code examples and all.

If your question is really about the "best way", and since you do not provide much detail about your environment, the most interesting answer may be that which in turn contains a link to Best way to copy between two Stream instances.

Other things to consider:

  • ContentType should be "application/pdf"
  • The "Content-Disposition" header will let you manage weather or not the PDF should be displayed "inline" in the browser, or as an "attachment", resulting in a download trigger.
  • "Content-Disposition" is also where you manage the file name for the PDF as the user decides to download it.
  • Remember closing and disposing all streams you open, either explicitly or by utilizing the "using" pattern.

Upvotes: 1

Related Questions