简体   繁体   English

我想在C#中保存HTML代码

[英]I want to code for save html in C#

I am using 'save as html' to save file as html. 我正在使用“另存为html”将文件另存为html。 My code is: 我的代码是:

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);
}

I am getting the following error in response.End(); 我在response.End();收到以下错误response.End(); line. 线。

Enable to evaluate expression because the code is optimized or a native frame is on top of the call stack. 启用评估表达式,因为代码已优化或本机框架位于调用堆栈的顶部。

If you use the Response.End a ThreadAbortException exception occurs. 如果使用Response.End则会发生ThreadAbortException异常。 You can use a try-catch statement to catch this exception. 您可以使用try-catch语句来捕获此异常。

The Response.End method ends the page execution and shifts the execution to the Application_EndRequest event in the application's event pipeline. Response.End方法结束页面执行,并将执行转移到应用程序的事件管道中的Application_EndRequest事件。 The line of code that follows Response.End is not executed. Response.End的代码行未执行。

Check Here 在这里检查

Also check this Article and this 还要检查这篇文章这个

-- Hope it helps . - 希望能帮助到你 。

To work around this problem, use one of the following methods: 要变通解决此问题,请使用下列方法之一:

For Response.End , call the HttpContext.Current.ApplicationInstance.CompleteRequest method instead of Response.End to bypass the code execution to the Application_EndRequest event. 对于Response.End ,请调用HttpContext.Current.ApplicationInstance.CompleteRequest方法而不是Response.End,以将代码执行绕过Application_EndRequest事件。

For Response.Redirect , use an overload, Response.Redirect(String url, bool endResponse) that passes false for the endResponse parameter to suppress the internal call to Response.End. 对于Response.Redirect ,请使用重载Response.Redirect(String url,bool endResponse),为endResponse参数传递false,以禁止对Response.End的内部调用。

For example: 例如:

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

If you use this workaround, the code that follows Response.Redirect is executed. 如果您使用此替代方法,则会执行Response.Redirect之后的代码。 For Server.Transfer, use the Server.Execute method instead. 对于Server.Transfer,请改用Server.Execute方法。

Got from this link http://support.microsoft.com/kb/312629/en-us 从此链接http://support.microsoft.com/kb/312629/en-us

response.redirect also faces the same problem you should be adding false response.redirect也面临您应该添加false的相同问题

but in case if you are downloading word document file response.end() should be used, if not the word document downloaded will be in corrupted format. 但是如果要下载Word文档文件,则应使用response.end(),否则,下载的Word文档格式将损坏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM