An ActionResult
can return FileContentResult
by specifying file path and file type based from extension definition, known as MIME type.
The MIME type can be set automatically depending on file type using GetMimeMapping
method, or defined manually in proper format, e.g. "text/plain".
Since FileContentResult
requires a byte array to be returned as a file stream, System.IO.File.ReadAllBytes
can be used to read file contents as byte array before sending requested file.
public class FileController : Controller
{
public ActionResult DownloadFile(String fileName)
{
String file = Server.MapPath("~/ParentDir/ChildDir" + fileName);
String mimeType = MimeMapping.GetMimeMapping(path);
byte[] stream = System.IO.File.ReadAllBytes(file);
return File(stream, mimeType);
}
}