English 中文(简体)
MVC:如何发布用户选择并用PDF回复
原标题:MVC: how to post back user selection and respond with PDF

这应该是一个简单的答案,但由于我是MVC的新手,我还不完全知道该怎么做:我想通过向浏览器发送PDF来响应用户点击链接。

  • 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.

假设我的控制器名为Invoice,方法为DownloadInvoice,那么我应该在视图中放些什么来发回ID(字符串),以及我如何响应此回发?

我能简单地回答一下吗

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

或者我需要将查看器引导到另一个具有完整

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

最佳回答

不需要其他视图。控制器操作上的FileStreamResult应该足够了(您可能希望使用[Post]属性过滤此类操作)。

问题回答

你为什么不试试?

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

返回一个文件流会按原样返回:一个文件。这不会向浏览器发送任何标头或任何内容来告诉它加载另一个url。





相关问题
Prevent Postback when user clicks browser s back button

I have a web page that sends email to multiple users (online distribution list). After the submit button is clicked and the email is sent, a status page is shown listing how many emails were sent, ...

An update panel, a postback and jQuery

An update panel, a postback and jQuery. Sounds like a bad joke, but here s my situation. I ve got two grids wrapped up in a MS update panel. The grids each have buttons in them that cause postback ...

ASP.NET - Control Events Not Firing Inside Repeater

This is a absurdly common issue and having exhausted all of the obvious solutions, I m hoping SO can offer me some input... I have a UserControl inside a page which contains a repeater housing several ...

How do I make a Textbox Postback on KeyUp?

I have a Textbox that changes the content of a dropdown in the OnTextChanged event. This event seems to fire when the textbox loses focus. How do I make this happen on the keypress or keyup event? ...

Postback after JQuery Dialog box closes causes js error in IE

I am creating JQuery dialog boxes and need the close functionality to cause a postback to the parent form (so that data changed in the popup form displays immediately on the parent screen). I have ...

How Do I Get a Dynamic Control s Value after Postback?

I have a listview that adds controls in the ItemDataBound Event. When the postback occurs, I cannot find the new controls. After a bit of research, I found that ASP .NET needs these controls created ...

ASP.Net s auto-postback. What happens when its too slow?

I am making a web application. I have gotten a weird error with update panels. Ok, so say you have two update panels and each update panel has a textbox in it. Both of these textboxes are auto-...

热门标签