【问题标题】:Bootstrap Modal Won't Hide After Downloading Excel File下载 Excel 文件后,Bootstrap 模式不会隐藏
【发布时间】:2019-01-21 07:35:39
【问题描述】:

我正在使用 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' in 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>

【问题讨论】:

  • 尝试用HttpContext.Current.ApplicationInstance.CompleteRequest();替换Response.End()
  • 我的新英雄!!感谢您抽出宝贵的时间来帮助一位程序员!
  • 查看此链接。它更详细地解释了您的问题stackoverflow.com/questions/20988445/…

标签: c# asp.net bootstrap-4 bootstrap-modal npoi


【解决方案1】:

if Response.End() 与 Response.Flush() 一起使用时抛出异常。将 Response.End 替换为这些行

HttpContext.Current.Response.Flush();
HttpContext.Current.Response.SuppressContent = True;
HttpContext.Current.ApplicationInstance.CompleteRequest();

【讨论】:

    猜你喜欢
    • 2021-11-24
    • 1970-01-01
    • 1970-01-01
    • 2018-08-03
    • 1970-01-01
    • 2016-05-04
    • 1970-01-01
    • 1970-01-01
    • 2016-06-24
    相关资源
    最近更新 更多