移动应用程序下载站点
问题描述:
我们创建了一个CMS,它运行良好,但现在我想将移动二进制文件(安装程序)文件下载到CMS。他们目前正从另一台服务器流式传输。移动应用程序下载站点
我能看到的唯一的解决办法是有一个什么样的文件是什么文件夹等为一个XML文档,并使用Linq2Xml检索文件并将其串流到手机浏览器的索引。我真的不想为此使用数据库。我正在考虑将下载门户升级到MVC,因为内置了通过指定byte [],文件名和MIME将文件直接传输到浏览器的功能。
有什么更好的建议吗?
答
非常简单,直接从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