2013-12-12 40 views
0

我使用'另存爲html'將文件另存爲html。 我的代碼是:我想代碼保存html中的C#

string fileName = ""; 
string htmlpfilepath; 
string[] parts; 
try 
{ 
    htmlpfilepath = Server.MapPath(ClinicalDocFile); 
    parts = ClinicalDocFile.Split('/'); 

    if (parts.Length > 0) 
     fileName = parts[parts.Length - 1]; 

    HttpResponse response = HttpContext.Current.Response; 
    response.Clear(); 
    response.AddHeader("Content-Type", "text/HTML"); 
    response.AddHeader("Content-Disposition", 
     "attachment; filename=" + fileName + "; size=" + fileName.Length.ToString()); 
    response.Flush(); 
    response.WriteFile(htmlpfilepath); 
    response.Flush(); 
    response.End(); 
} 
catch (Exception ex) 
{ 
    FW.Fission.WebSite.Code.Utilities.SiteUtilities.HandleException(ex); 
} 

我收到以下錯誤response.End();線。

啓用以評估表達式,因爲代碼已經過優化或本機框架位於調用堆棧之上。

+3

這不是程序錯誤。這是一個調試器問題。你在調試模式下運行嗎? – Gusdor

+1

它只告訴你它不能告訴你真正的錯誤是什麼。在你的項目屬性 - >構建檢查「優化代碼」,然後看看有什麼錯誤說。當然,你必須重建 –

+0

我得到同樣的錯誤。 –

回答

0

如果使用Response.End會發生ThreadAbortException異常。您可以使用try-catch語句來捕獲此異常。

Response.End方法結束頁面執行並將執行轉移到應用程序的事件管道中的Application_EndRequest事件。下面的代碼行Response.End未被執行。

檢查Here

還要檢查這個Articlethis

- 希望它幫助。

0

要解決此問題,使用下列方法之一:

對於Response.End,調用HttpContext.Current.ApplicationInstance.CompleteRequest 方法,而不是要到Response.End繞過代碼執行到Application_EndRequest事件。

對於Response.Redirect,使用一個重載,Response.Redirect(String url,bool endResponse)爲endResponse參數傳遞false以禁止對Response.End的內部調用。

例如:

Response.Redirect ("nextpage.aspx", false); 

如果您使用此解決方案,下面的Response.Redirect執行代碼。 對於Server.Transfer,請改用Server.Execute方法。

接到了這個鏈接 http://support.microsoft.com/kb/312629/en-us

response.redirect也面臨着,你應該加入假

但萬一如果你正在下載的Word文檔文件到Response.End()應使用如果不是同樣的問題下載的Word文檔將以損壞的格式顯示。

相關問題