awj
awj

Reputation: 7949

MVC: how to post back user selection and respond with PDF

This should be a simple answer, but as I'm new to MVC I don't fully know my way around just yet: I want to respond to the user clicking a link by sending a PDF down to the browser.

Assuming my controller is called Invoice and the method is DownloadInvoice, what should I put on my view to post the IDs back (string), and how do I respond to this postback?

Can I simply respond with this

return new FileStreamResult(generatedInvoice.Stream, "application/pdf");

Or do I need to direct the viewer to another view which has the full

Response.Clear();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=labtest.pdf");
Response.Buffer = true;
Model.Stream.WriteTo(Response.OutputStream);
Response.End();

?

Upvotes: 1

Views: 364

Answers (2)

Oded
Oded

Reputation: 498942

No need for another view. The FileStreamResult should be enough on a controller action (you may want to filter such an action with a [Post] attribute).

Upvotes: 1

Shawn Mclean
Shawn Mclean

Reputation: 57469

Why didn't you just try it?

return new FileStreamResult(generatedInvoice.Stream, "application/pdf");

Returning a filestream returns as it say: a file. This does not send any header or anything to the browser to tell it to load up another url.

Upvotes: 1

Related Questions