您可以將控制器添加到服務器的文件中。
public class FileController : Controller
{
private IFileStore _fileStore;
public FileController(IFileStrore fileStore)
{
this._fileStore = fileStore;
}
public ActionResult Index(string fileName)
{
// Do a database look up if the user has permission
if (_fileStore.HasPermission(fileName, User))
{
// Flush the file content if the user has permission
var myfile = _fileStore.GetFile(fileName);
Response.ClearContent();
Response.AddHeader("Content-Disposition", "attachment; filename=" + myfile.FileName);
Response.AddHeader("Content-Length", myfile.Length.ToString());
Response.ContentType = myfile.Extension.ToLower();
Response.WriteFile(myfile.FullName, false);
Response.Flush();
Response.Close();
}
else
{
// Return a Access Denied page
return View("NoPermission");
}
}
}
這篇文章將幫助你http://prideparrot.com/blog/archive/2012/6/authorizing_folders_using_routeexistingfiles – VJAI 2012-07-31 09:40:10