Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the MVC way of simultaneously sending a file and redirecting to a new page?

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");
    }
like image 502
Sailing Judo Avatar asked Jul 06 '09 14:07

Sailing Judo


1 Answers

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 :)

like image 121
Sekhat Avatar answered Nov 26 '22 09:11

Sekhat