开发者

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

This should be 开发者_如何学Pythona 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 FileStreamResult should be enough on a controller action (you may want to filter such an action with a [Post] attribute).


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜