非常简单,直接从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