You should have already seen that the methods of the controllers that meet the requests of the pages return objects of type ActionResult
.
Normally you return HTML content through the View
method of the controller itself. You can change the return type to a file easily, just return a FileStreamResult
object instead of calling the View
method.
The official documentation .
And an example:
public ActionResult BoloDeFuba()
{
FileStream arquivo = new FileStream(@"c:\bolo de fubá.doc");
FileStreamResult download = new FileStreamResult(arquivo, "application/msword"); // O segundo parâmetro é o Mime type
download.FileDownloadName = "bolo de fubá.doc";
return download;
}
Note that FileStreamResult
receives any object of type Stream
. You can mount a file in memory or load it from the database instead of loading from a folder, when you need it or more convenient.