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.
- I have a string of IDs on the page that I need to post back to the method so that it can generate the requested content.
- I can build the PDF as a memory stream exposed to the controller.
- I want the user to stay on the same page on which they clicked the link and be presented with the “Save to…” dialog.
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();
?
No need for another view. The
FileStreamResultshould be enough on a controller action (you may want to filter such an action with a[Post]attribute).