2012-06-07 40 views
0

我有一個控制器和下載文件的方法。沒有代碼,我需要的只是去這個鏈接:從我的項目中下載文件

http://localhost:1186/Content/MyFolder/file1.exe 

和文件被下載。

我試着用代碼來做到這一點是這樣的:

Response.Redirect(Server.MapPath("~\\Content\\MyFolder\\file1.exe")); 

但斷點通過此行並沒有任何反應。我認爲問題是我正在使用Server.MapPath,但我還會如何做到這一點?

+0

Server.Ma pPath返回資源的物理路徑,請嘗試Response.Redirect(「〜\\ Content \\ MyFolder \\ file1.exe」); – MSUH

+0

此相關的有關下載使用MVC文件可能是有用的,這樣的問題,http://stackoverflow.com/questions/5826649/returning-a-file-to-view-download-in-mvc – Despertar

回答

0

你可以做這樣的事情

創建一個動作

public ActionResult RedirectToDownload() 
{ 
    return View(); 
} 

,然後上載使用JavaScript的看法重定向到URL

$(document).ready(function() { 
    window.navigate("~/Content/MyFolder/file1.exe"); 
}); 

您也可以使用此

public ActionResult Index() 
{ 
    return Redirect("~/Content/MyFolder/file1.exe"); 
} 
+0

我看不到的點讓控制器將用戶發送給重新定向用戶的視圖。這是控制器執行重定向的主要工作。 – Despertar

+0

你仍然需要一個視圖,除非你是直接從動作下載過文件 –

+0

@Despertar http://blogs.msdn.com/b/rickandy/archive/2012/03/01/response-redirect-and-asp-net -mvc-拒收mix.aspx –