2009-04-16 71 views
0

我們創建了一個CMS,它運行良好,但現在我想將移動二進制文件(安裝程序)文件下載到CMS。他們目前正從另一臺服務器流式傳輸。移動應用程序下載站點

我能看到的唯一的解決辦法是有一個什麼樣的文件是什麼文件夾等爲一個XML文檔,並使用Linq2Xml檢索文件並將其串流到手機瀏覽器的索引。我真的不想爲此使用數據庫。我正在考慮將下載門戶升級到MVC,因爲內置了通過指定byte [],文件名和MIME將文件直接傳輸到瀏覽器的功能。

有什麼更好的建議嗎?

回答

1

非常簡單,直接從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); 
    } 
} 
+1

不是太寒酸,但有一個內置該MVC的函數調用FileContentResult,用法: 返回新FileContentResult(字節,「X-EPOC/X-SISX -app「); – mhenrixon 2009-04-16 14:50:40