Save file dialog in MVC

blackik picture blackik · Jul 25, 2011 · Viewed 8.6k times · Source

How to create save file dialog in MVC application? I couldn't find any example.

Thanks in advance.

Answer

Darin Dimitrov picture Darin Dimitrov · Jul 25, 2011

By using the Content-Disposition header to attachment when returning the file to download:

public ActionResult Download()
{
    return File(@"c:\work\report.pdf", "application/pdf", "reoprt.pdf");
}

Or if the file to download is dynamically generated:

public ActionResult Download()
{
    byte[] pdf = ... get the contents of the report
    return File(pdf, "application/pdf", "reoprt.pdf");
}