我正在使用NPOI允许用户从我的网站(asp.net/C#/bootstrap 4)下载Excel文件。实际下载文件的部分如下所示:
//*****************************************
//* Workbook Download & Cleanup
//*****************************************
using (var stream = new MemoryStream())
{
Response.Clear();
wb.Write(stream);
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", "Behavior Stats YTD.xlsx"));
Response.BinaryWrite(stream.ToArray());
Response.Flush();
Response.End();
}
在调试模式下,一旦代码执行Response.End(),我将收到以下错误:
引发的异常:“ System.Threading.ThreadAbortException” mscorlib.dll
其他信息:线程正在中止。
我之前曾问过这个问题,并向我解释说这有效地杀死了线程,到此为止,将不执行任何命令。解决方法是,我被告知将下载功能放在单独的.aspx页上,以便杀死的线程在该页上,而不是在我的“主”页上。
一旦选择了“导出到Excel”选项,我就会调用JavaScript,如下所示:
function beginDownload() {
$('#progressModal').modal('show');
var downloadFrame = document.createElement("IFRAME");
if (downloadFrame != null) {
downloadFrame.setAttribute("src", 'behaviorExport.aspx?exportType=' + exportType + '&exportMedia=' + exportMedia);
downloadFrame.style.width = "0px";
downloadFrame.style.height = "0px";
document.body.appendChild(downloadFrame);
}
var myDownload = setInterval(function () {
if (Cookies.get('behaviorAnalysisDownloadCookie') == null) {
}
else {
Cookies.remove('behaviorAnalysisDownloadCookie');
$('#progressModal').modal('hide');
clearInterval(myDownload);
}
}, 1000);
}
如您所见,我正在调用IFrame并从单独的.aspx页启动导出代码,但是显然我仍然遇到相同的问题。我的progressModal模态会打开并显示我的进度条,但不会自动关闭。文件导出完成并且模态消失后,我可以单击屏幕,但是我不希望我的用户知道这样做。有人对我做错了什么建议吗?
我的模式html是:
<!-- Modal -->
<div class="modal fade" id="progressModal" tabindex="-1" role="dialog"
aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="progress" style="height: 25px">
<div class="progress-bar progress-bar-striped progress-bar-animated bg-warning"
role="progressbar" aria-valuenow="75" aria-valuemin="0" aria-valuemax="100"
style="width: 75%; color: black">
...Building File
</div>
</div>
</div>
</div>
</div>
答案 0 :(得分:0)
if Response.End()与Response.Flush()一起使用时引发异常。用这些行替换Response.End
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.SuppressContent = True;
HttpContext.Current.ApplicationInstance.CompleteRequest();