0

I am letting user download file using following code. It works fine but also generates error and source for error is Response.End();

Error Message :Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack.`

Below is my code. How can i handle this error and is this error not freeing my resources which can result in unwanted memory use.

I use this for asp.net webform application.

    try
    {
        string fileName = Request["file_ID"];
        string path = Server.MapPath("~/App_Data/uploads/"+fileName);


        //string = Server.MapPath(strRequest); 
        System.IO.FileInfo file = new System.IO.FileInfo(path);
        if (file.Exists)
        {
            Response.Clear();
            Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
            Response.AddHeader("Content-Length", file.Length.ToString());
            Response.ContentType = "application/"+file.Extension;
            Response.WriteFile(file.FullName);
            Response.End();

            // System.Threading.Thread.Sleep(2000);
           // Page.ClientScript.RegisterStartupScript(this.GetType(), "MyScript", "alert('aa')", true);

        }
        else
        {
            //Page.ClientScript.RegisterStartupScript(this.GetType(), "MyScript", "myWindow.close();", true);
        }
    }


    catch (Exception rt)
    {
         Response.Write(rt.Message);
    }
}

I was looking at this solution but i am not sure how i can implement it in my code.

Unable to evaluate expression... on web page

UPDATE:

I actually want user to download file and close same with using code behind script which are commented in code right now.

So i am not sure how to better optimize this code to handle the exception generated by respond.end statement.

I tried to use Page.ClientScript.RegisterStartupScript(this.GetType(), "MyScript", "myWindow.close();", true); but it doesn't work either.

Thanks

Community
  • 1
  • 1
Learning
  • 19,469
  • 39
  • 180
  • 373

2 Answers2

1

The other answer seems to be saying you can either not use Response.End as it is unnecessary, or add a catch for ThreadAbortException where you have your current catch

The question links to another question that suggests adding the following:

// Sends the response buffer
Response.Flush()

// Prevents any other content from being sent to the browser
Response.SuppressContent = TrueResponse.SuppressContent = True
Community
  • 1
  • 1
PMC
  • 4,698
  • 3
  • 37
  • 57
  • I think better approach use advices suggested by users and also use .ashx file for this then i dont need to work about closing window. for any exception error i will redirect them to error page. – Learning Oct 06 '13 at 08:06
1

use HttpContext.Current.ApplicationInstance.CompleteRequest

OR TRY SOMETHING LIKE

Response.OutputStream.Flush()
Response.OutputStream.Close()
Response.Flush()
Response.End() 

READ Is Response.End() considered harmful?

Community
  • 1
  • 1
internals-in
  • 4,798
  • 2
  • 21
  • 38
  • I think better approach use advices suggested by users and also use .ashx file for this then i dont need to work about closing window. for any exception error i will redirect them to error page. – Learning Oct 06 '13 at 08:06