ASP.NET MVC 如何从控制器操作中输出 FileContent 并继续
我对 MVC 相当陌生,想知道正确的方法是什么 - 提交表单时执行编辑 ActionResult,保存数据后,我想生成一个文件并发送到浏览器进行下载,但也继续另一个动作。
文件被发送到浏览器,但控制器中没有进行进一步处理,因为我使用了 return RedirectToActions。预先感谢您的任何指点。
public ActionResult Edit(int id, InvoiceFormData formData)
{
...
return base.RedirectToAction("ReturnPdf", new { id = 99 });
// More processing...ideally...then
return base.RedirectToAction("Action", "Controller");
}
public FileResult ReturnPdf(int? id)
{
...
return output; // writes file output and prompts user to download
}
I am fairly new to MVC and want to know what the correct approach is - when a form is submitted the Edit ActionResult is executed, once the data is saved, I want to generate a file and send to the browser for download but also continue to another action.
The file is sent to the browser but no further processing occurs in the controller, because I use return RedirectToActions. Thanks in advance for any pointers.
public ActionResult Edit(int id, InvoiceFormData formData)
{
...
return base.RedirectToAction("ReturnPdf", new { id = 99 });
// More processing...ideally...then
return base.RedirectToAction("Action", "Controller");
}
public FileResult ReturnPdf(int? id)
{
...
return output; // writes file output and prompts user to download
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不能在同一操作结果中使用两个 return 语句并期望它们都运行。当您“返回”时,意味着您正在返回输出,然后退出操作结果。因此,您可以链接您的操作,这样您可以将该逻辑移至 ReturnPdf AR 中(因为这就是我在代码中看到的内容),而不是在编辑操作结果中进行进一步处理,然后最终返回最终输出无论您达到什么行动结果。我希望这已经足够清楚了......
You cannot use two return statements in the same action result and expect them to both run. When you "return" it means you're returning the output and then quiting out of the action result. Therefore, you could kind of chain your actions, so rather than doing the further processing inside the Edit action result, you could move that logic into the ReturnPdf AR(since that's what's I could see in your code), then finally return the final output in whatever action result you landed on. I hope that was clear enough...