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();
}