简体   繁体   中英

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.

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. Imagine the security vulnerability caused by what you are proposing.

Yes, it's possible, but you don't use FileResult. If you set the content type and disposition headers to something unknown , the browser will prompt the user to save the result.

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. So I have a WordResult like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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