tags:

views:

193

answers:

3

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

Cheers.

A: 

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.

liammclennan
I think its just like templating. The data is saved on the webserver, from which it comes from anyway.
Schotime
I misunderstood. I thought you wanted to save the file on the client's machine.
liammclennan
A: 

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.

Craig Stuntz
A: 

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

Hope this helps

Trevor de Koekkoek
He wants to save the file on the webserver, not the client, so your answer doesn't apply.
Tilendor