Very simple to provide files directly from an MVC controller. Here's one I prepared earlier, as it were:
[RequiresAuthentication]
public ActionResult Download(int clientAreaId, string fileName)
{
CheckRequiredFolderPermissions(clientAreaId);
// Get the folder details for the client area
var db = new DbDataContext();
var clientArea = db.ClientAreas.FirstOrDefault(c => c.ID == clientAreaId);
string decodedFileName = Server.UrlDecode(fileName);
string virtualPath = "~/" + ConfigurationManager.AppSettings["UploadsDirectory"] + "/" + clientArea.Folder + "/" + decodedFileName;
return new DownloadResult { VirtualPath = virtualPath, FileDownloadName = decodedFileName };
}
You might need to do a bit more work actually deciding what file to deliver (or, more likely, do something completely different), but I've just cut it down to the basics as an example that shows the interesting return bit.
DownloadResult is a customised ActionResult:
public class DownloadResult : ActionResult
{
public DownloadResult()
{
}
public DownloadResult(string virtualPath)
{
VirtualPath = virtualPath;
}
public string VirtualPath { get; set; }
public string FileDownloadName { get; set; }
public override void ExecuteResult(ControllerContext context)
{
if (!String.IsNullOrEmpty(FileDownloadName))
{
context.HttpContext.Response.AddHeader("Content-type",
"application/force-download");
context.HttpContext.Response.AddHeader("Content-disposition",
"attachment; filename=\"" + FileDownloadName + "\"");
}
string filePath = context.HttpContext.Server.MapPath(VirtualPath);
context.HttpContext.Response.TransmitFile(filePath);
}
}