我们有一个我创建的CMS,它工作得很好,但现在我想将移动二进制(安装程序)文件的下载移动到CMS。它们当前是从另一台服务器流式传输的。
我所能看到的唯一解决方案是将哪些文件放在哪些文件夹中等作为Xml文档进行索引,并使用Linq2Xml来检索文件并将它们流式传输到移动浏览器。我真的不想为此使用数据库。我正在考虑将下载门户升级到MVC,因为内置的功能可以通过指定byte[]、文件名和mime直接将文件流式传输到浏览器。
有什么更好的建议吗?
发布于 2009-04-16 14:21:17
直接从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);
}
}https://stackoverflow.com/questions/756189
复制相似问题