2012-11-26 21 views
0

我有下载链接存在于网格视图中,当我点击它时,会弹出一个保存对话框,并且excel填充将会是下载。 但我收到错误“无法评估表达式,因为代码已优化或本机框架位于调用堆栈之上。”在Response.End();.无法评估表达式,因为代码已经优化或者本地框架位于调用堆栈顶部

请帮帮我。提前致谢。

代码:

protected void grdFiles_RowCommand(object sender, GridViewCommandEventArgs e) 
    { 

     try 
     { 
      if (e.CommandName == "download") 
      { 
       string _FileName = Convert.ToString(e.CommandArgument); 
       //Response.Clear(); 
       //Response.AppendHeader("Content-Disposition", "attachment; filename=" + _FileName); 
       //Response.ContentType = "application//octet-stream"; 
       //Response.TransmitFile(Server.MapPath("~/Files/" + _FileName)); 
       //Response.End(); 

       // Get the physical Path of the file(test.doc) 
       string filepath = Server.MapPath("test.doc"); 

       // Create New instance of FileInfo class to get the properties of the file being downloaded 
       FileInfo file = new FileInfo(Server.MapPath("~/Files/" + _FileName)); 

       // Checking if file exists 
       if (file.Exists) 
       { 
        // Clear the content of the response 
        Response.ClearContent(); 

        // LINE1: Add the file name and attachment, which will force the open/cance/save dialog to show, to the header 
        Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name); 

        // Add the file size into the response header 
        Response.AddHeader("Content-Length", file.Length.ToString()); 

        // Set the ContentType 
        Response.ContentType = "application/vnd.ms-excel"; 

        // Write the file into the response (TransmitFile is for ASP.NET 2.0. In ASP.NET 1.1 you have to use WriteFile instead) 
        Response.TransmitFile(file.FullName); 

        // End the response 
        Response.End(); 
       } 
      } 
     } 
     catch (Exception ex) 
     { 

     } 
    } 

回答

-1

存储在Web.config文件中的文件路径,并尝试这种代码:

string filename = "test.doc"; 
    string FilePath = ConfigurationManager.AppSettings.Get("SharedPath") + ConfigurationManager.AppSettings.Get("Path") + "\\\\" + filename; 

    FileInfo file = new FileInfo(FilePath); 
    if (file.Exists) 
    { 
     Response.AppendHeader("content-disposition", 
     "attachment; filename=" + filename); 
     Response.ContentType = "application/download"; 
     Response.WriteFile(FilePath); 
     Response.End(); 
    } 
0

这是因为ThreadAbortException的。尝试处理这个特定的异常。

try 
    { 
     if (file.Exists) 
     { 
     //do something 
     } 
     response.End(); 
    } 
    catch (ThreadAbortException ex) 
    { 
     //Log trace info 
    } 
2

此错误的另一个可能的原因,可能是因为您的网格是在更新面板?

如果是这样的话我建议你添加网格控件作为一个回触发这样:

<asp:UpdatePanel runat="server" ID="UpdatePanel1"> 
    <Triggers> 
     <asp:PostBackTrigger ControlID="grdFiles" /> 
    </Triggers> 
    <ContentTemplate> 
     <gridview ID="grdFiles" runat="server"> 
     your grid view content 
     </gridview> 
    </ContentTemplate> 
</asp:UpdatePanel> 

虽然把整个网格作为一个回触发可能是矫枉过正(后背上的可能会发生分页等),您可以尝试将您的下载链接创建为模板列,并将网格内的控件设置为回发触发器。

我有一个类似的问题,我使用导出按钮作为下载按钮,并使用网格命令来选择一个细节,并使该按钮仅在网格中的某些东西被选中时可用,然后将回发触发器置于这个按钮,而不是在网格上。

0

有一个方便的方法来处理“无法评估表达式,因为代码已经优化或本机框架位于调用堆栈之上。”的问题。您需要在输出窗口上书写。

使用System.Diagnostics添加;

添加一个try/catch对于被示数

在搭上线添加这些行

try 
{ ..} 
catch(Exception ex) 
{ 
    Debug.WriteLine(ex.Message); 
    Debug.WriteLine(ex.StackTrace); 
    Debug.WriteLine(ex.InnerException.ToString()); 
} 

只是调试和检查输出窗口

希望它能帮助。

相关问题