2016-01-21 104 views
1

在我的页面中,我正在尝试下载文件。该文件下载成功,但我得到System.Threading.ThreadAbortException。所以我在我的尝试Catch Block中处理了这个错误,并将错误标签设置为空白,但它不会在页面中更新。标签在System.Threading.ThreadAbortException中未更新

 catch (System.Threading.ThreadAbortException) 
     { 
      lblErrorMsg.Text = "dfdf"; 
     } 
     catch (Exception ex) 
     { 
      lblErrorMsg.Text = "Error while processing you request :<br> Erorr Description : " + ex.Message; 
     } 

这是我写文件功能

public static void WriteFile(string nameOfFile, string fileContent, HttpResponse writer) 
    { 
     writer.ClearHeaders(); 
     writer.ClearContent(); 

     writer.ContentType = "text/xml"; 
     writer.AppendHeader("Content-Disposition", "attachment; filename=" + nameOfFile); 
     writer.Write(fileContent); 
     writer.Flush(); 
     writer.End(); 
    } 

谁能告诉为什么即使谈到system.thread.threadabortexceptiopn的catch块下,当我调试的代码标签没有被设置为空白?

回答

0

ThreadAbortException发生是因为您通过调用Response对象的End()方法提前关闭Response。 这也解释了为什么写在页面内容上为时已晚。这不是一个非常烦人的错误,但它会更好地处理干净。

只要检查这些答案Why Response.Redirect causes System.Threading.ThreadAbortException?How to Avoid Response.End() "Thread was being aborted" Exception during the Excel file download和其他与Response和ThreadAbortException有关的答案,就可以根据您的使用情况编写一个更好的文件下载代码来理解它并正确处理。

另外请注意,对于页面和其上的一些内容(如标签),同时具有完全重写的响应流并不很有意义。

+0

我没有在标签上写任何内容。只需在标签上写入例外。在你的答案中的链接不帮助我:(你能给我具体的解决方案 – Happy

+0

也正如你可以在我的代码中看到,我没有做response.redirect – Happy

+0

第一个问题:你明确地清空响应内容来取代你的下载,第二个问题:你有没有在第二个链接上看到user3412640的回答?我告诉过你,问题在于对End()的调用,你的回应的方法 – AFract