非常簡單,直接從MVC控制器提供文件。這裏有一個我提前準備好了,因爲它是:
[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 };
}
你可能需要做更多的工作實際決定提供哪些文件(或者,更可能的是,這樣做完全不同的事情),但我剛切它作爲一個例子顯示了有趣的回報位。
DownloadResult是一個定製的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);
}
}
不是太寒酸,但有一個內置該MVC的函數調用FileContentResult,用法: 返回新FileContentResult(字節,「X-EPOC/X-SISX -app「); – mhenrixon 2009-04-16 14:50:40