-1

My aspx page has a Download link. On click of it a popup occurs asking user to enter some data based on which the file is modified before getting downloaded.

I have forwarded the request made by the pop up to a generic handler which is having code to generate a file and append it into the response header.

Now, I am getting a thread abort exception as Could not able to evaluate expression because code is optimized or a native frame is on top of call stack.

I guess the response header was not interpreted by the client because of the pop up. Please find the code snippet and tell me where I am going wrong :

ASPX for Popup Button:

 <button id="BtnDownload" class="btnSubmit" onclick="javascript:downloadFileViaHandler()">
                                        Download</button>

Javascript Function:

function downloadFileViaHandler() {
$('#popupdiv').dialog('close');
var urlHandler = "/WebHandlers/downloadHandler.ashx";
var urlHandlerParam = window.location.href.split('/');
var actualUrlparam = urlHandlerParam[0] + "/" + urlHandlerParam[1] + "/" + urlHandlerParam[2] + "/" + urlHandlerParam[3];
//Sanitize URLs for Server     
var urlHandler = actualUrlparam + urlHandler;
 $.ajax({
  type: 'POST',
  async: false,      
  url: urlHandler,
  data: { FileID: '' + FileID + '\'' },
  success: function (data) {
      console.info(data);
      },
  error: function () {
      alert("Error occured while Downloding File");
  }
  });

}

Handler Code:

 public void ProcessRequest(HttpContext context)
 {  
   //some Logic to generate file//

 string filePath = str_SaveFileLocation;
 DownloadFilePath = (str_SaveFileLocation); 
 string name = Path.GetFileName(DownloadFilePath);
 string ext = Path.GetExtension(DownloadFilePath); 
 string Filetype = "text/DBC";
 context.Response.Clear();
 context.Response.AppendHeader("content-disposition", "Downloads;   filename=" + name);
 context.Response.ContentType = Filetype;
 context.Response.WriteFile(DownloadFilePath);
 context.Response.End();
}

1 Answers1

0

A ThreadAbortException is normal when you call Response.End(). See documentation.

To mimic the behavior of the End method in ASP, this method tries to raise a [ThreadAbortException] exception. If this attempt is successful, the calling thread will be aborted, which is detrimental to your site's performance. In that case, no code after the call to the End method is executed. If the End method is not able to raise a [ThreadAbortException], it instead flushes the response bytes to the client. It does this synchronously, which can also be detrimental to your site's performance.

mason
  • 31,774
  • 10
  • 77
  • 121