简体   繁体   English

新的ActionResult可以呈现生成的视图并通过电子邮件发送内容

[英]new ActionResult that can render the view generated and send contents via email

I was wondering if it were possible to write a file FileResult that worked something like this. 我想知道是否有可能编写一个像这样的文件FileResult。

public ActionResult Generate(int id)
{
    //Fill Model
    string ViewName = "template1";    

    return new FileResult(ViewName, @"c:\save"+ id +".txt");
}

where it would take the model, and combine it with the view to give you the output that would normally be displayed to the screen but instead just save it with the filename specified. 在其中放置模型的位置,并将其与视图结合在一起,以提供通常显示在屏幕上的输出,而只是将其保存为指定的文件名。

Note: This file will be saved on the webserver 注意:此文件将保存在网络服务器上

I don't think it is possible. 我认为不可能。 You can specify the filename and the mime type but that is it. 您可以指定文件名和mime类型,仅此而已。 Imagine the security vulnerability caused by what you are proposing. 想象一下您所提议的安全漏洞。

Yes, it's possible, but you don't use FileResult. 是的,有可能,但是您不使用FileResult。 If you set the content type and disposition headers to something unknown , the browser will prompt the user to save the result. 如果将内容类型和处置标头设置为unknown ,浏览器将提示用户保存结果。

As has been mentioned you can't do that for security reasons, but you can save a file if the user is prompted for saving the file. 如前所述,出于安全原因,您不能执行此操作,但是如果提示用户保存文件,则可以保存文件。 I have done that for my resume. 我已经为简历做好了。 I have a word version of my resume which is generated from xml. 我有从xml生成的简历的Word版本。 So I have a WordResult like this: 所以我有一个像这样的WordResult:

public class WordResult : ActionResult
{
public string FileName { get { return "Resume.doc"; } }

public override void ExecuteResult(ControllerContext context)
{
  GenerateMsWordDoc(context);
}

public void GenerateMsWordDoc(ControllerContext context)
{
  // You can add whatever you want to add as the HTML and it will be generated as Ms Word docs
  context.HttpContext.Response.AppendHeader("Content-Type", "application/msword");
  context.HttpContext.Response.AppendHeader("Content-disposition", "attachment; filename=" + FileName);

}

} }

To see this in action: http://www.aspevia.com/resume/show is the standard web version http://www.aspevia.com/resume/show?format=word is the word version 要查看实际效果: http : //www.aspevia.com/resume/show是标准的Web版本http://www.aspevia.com/resume/show?format=word是单词版本

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM