2011-12-22 72 views
1

爲了下載我寫了下面的代碼服務器無法HTTP標頭後清晰頭已被送往

[AcceptVerbs(HttpVerbs.Post | HttpVerbs.Get)] public ActionResult Download(string fileName) { 
    try { 
     string filepath = @"C:\shadow_copy.rar"; 

     Response.ClearHeaders(); 
     Response.Clear(); 
     Response.AddHeader("Content-Disposition", "attachment; filename= " + fileName.TrimEnd()); 
     Response.AddHeader("Content-Length", fileName.Length.ToString()); 
     Response.ContentType = "application/octet-stream"; 
     Response.WriteFile(filepath.TrimEnd()); 
     Response.End(); 

    } 
    catch (Exception exp) 
    { 
     // 
    } 

    return Redirect("http://mysite.com"); 
} 

我也用

一個文件,如果(!Response.IsRequestBeingRedirected)//將不叫的Response.Redirect(「http://www.google.com」)

但它顯示錯誤

「後,HTTP標頭已被送往不能重定向。」

+0

你想完成什麼?下載文件或重定向用戶。你必須選擇一個到另一個。 – 2011-12-22 09:40:42

回答

2

下載文件後無法重定向,您正試圖執行2個操作,只能執行第一個操作。

我建議你在新的(彈出)窗口中下載文件並根據需要重定向主頁面。

1

嘗試這樣的,這是一個容易一點:

[AcceptVerbs(HttpVerbs.Post | HttpVerbs.Get)] 
public ActionResult Download(string fileName) 
{ 
    string filepath = @"C:\shadow_copy.rar"; 
    return File(filepath, "application/octet-stream", "shadow_copy.rar"); 
} 

而且你不能下載後重定向。

相關問題