Loren Pechtel
Loren Pechtel

Reputation: 9093

How do I send a response and a file in ASP.NET MVC 3?

I've found a few things around on the web that say it can't be done but I've seen various websites do things that certainly look like it.

What I'm looking to do:

1) User clicks the submit button.

2) Website validates that everything is good with the request and sends back a page saying "I'm working on it".

3) When it's done crunching the website sends a zip file with the results and then replaces the "I'm working on it" page with the original.

I could fake step #2 by returning a page with a bunch of hidden fields and javascript to immediately push a hidden submit button, but that still doesn't redisplay the original when it's done crunching.

Upvotes: 1

Views: 545

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038850

You could return a File result:

public ActionResult DoTheCrunching()
{
    if (something is wrong)
    {
        // redisplay the view
        return View();
    }
    // otherwise compress and return the result in the response stream
    byte[] crunchedBuffer = ...
    var cd = new ContentDisposition
    {
        FileName = "foo.zip",
        Inline = false
    };
    Response.AppendHeader("Content-Disposition", cd.ToString());
    return File(crunchedBuffer, "application/zip");
}

Upvotes: 1

Related Questions