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