views:

317

answers:

1

I have a form which users must fill out and submit. The controller action does some work and decides the user can have a file and so redirects to another action which is a FilePathResult.

    [CaptchaValidator]
    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Index(FormCollection collection)
    {
        // do some stuff ...
        return RedirectToAction("Download");
    }


    [AcceptVerbs(HttpVerbs.Get)]
    public FilePathResult Download()
    {
        var fileName = "c:\foo.exe";
        return File(fileName, "application/octet-stream", "installer.exe");
    }

What I would like to do is redirect the user to another page which thanks the user for downloading the file but I'm not sure how to accomplish that in a "MVC-like" way.

The only way I can think of off the top of my head is to skip the Download action and instead redirect to the ThankYou action, and have the ThankYou view use javascript to send the file. But this just doesn't seem very MVC to me. Is there a better approach?

Results:

The accepted answer is correct enough but I wanted to show I implemented it.

The Index action changes where it redirects to:

        return RedirectToAction("Thankyou");

I added this controller (and view) to show the user any "post download information" and to say thanks for downloading the file. The AutoRefresh attribute I grabbed from link text which shows some other excellent uses.

    [AutoRefresh(ControllerName="Download", ActionName="GetFile", DurationInSeconds=3)]
    [AcceptVerbs(HttpVerbs.Get)]
    public ActionResult Thankyou()
    {
        return View();
    }

The action which get redirected to is this same as it was before:

    [AcceptVerbs(HttpVerbs.Get)]
    public FilePathResult GetFile()
    {
        var fileName = "c:\foo.exe";
        return File(fileName, "application/octet-stream", "installer.exe");
    }
+4  A: 

Just add a header to your response, in the action for your redirected page.

Googling came up with this header:

Refresh: 5; URL=http://host/path

In your case the URL would be replaced with the URL to your download action

As the page I was reading says, the number 5 is the number of seconds to wait before "refreshing" to the url.

With the file being a download, it shouldn't move you off your nice redirect page :)

Sekhat
Thanks... your answer lead me to some different research in which I found this page: http://weblogs.asp.net/rashid/archive/2009/04/29/fun-with-http-headers-in-asp-net-mvc-action-filters.aspx
Sailing Judo
Cool, glad I could help. I do like the attribute method. Does keep it looking nice. But it's no less MVC just to add headers manually if you require them.
Sekhat