1

我在网格视图中有下载链接,当我点击它时,会弹出一个保存对话框并下载 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)
        {

        }
    }
4

4 回答 4

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>

尽管将整个网格作为回发触发器可能会过大(可能会发生分页回发等),但您可以尝试将下载链接创建为模板列并将网格内的控件设置为回发触发器。

我有一个类似的问题,我使用导出按钮作为下载按钮,并使用网格命令选择一个详细信息并仅在选择网格中的某些内容时才使此按钮可用,然后在此按钮上放置一个回发触发器而不是把它放在网格上。

于 2014-01-22T11:26:27.953 回答
0

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

使用 System.Diagnostics 添加;

为出错的行添加 Try/Catch

在 Catch 添加这些行

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

只需调试并检查输出窗口

希望能帮助到你。

于 2014-09-03T02:29:11.993 回答
0

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

   try
   {
      if (file.Exists)
      {
         //do something
      }
      response.End();
   }
   catch (ThreadAbortException ex)
   {
       //Log trace info
   }
于 2012-11-26T12:15:44.897 回答
-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();
    }
于 2012-11-26T12:01:53.250 回答