0

I'm using NPOI to allow users to download an Excel file from my website (asp.net / C# / bootstrap 4). The part that actually downloads the file looks like this:

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

In debug mode, once the code executes Response.End() I get the following error:

Exception thrown: 'System.Threading.ThreadAbortException' in mscorlib.dll

Additional information: Thread was being aborted.

I've asked about this before and it was explained to me that this is effectively killing the thread and no commands past this point will execute. As a workaround I was told to place the download function on a separate .aspx page, so that the thread killed was on that page and not my 'main' page.

I have JavaScript that gets called once the Export to Excel option is selected that looks like this:

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

As you can see I'm calling an IFrame and launching the export code from a separate .aspx page, but I'm still having the same issue apparently. My progressModal modal opens up and displays my progress bar, but it does not automatically close. I can click on the screen after the file is finished exporting and the modal goes away, but I don't expect my users to know to do this. Does anybody have any suggestions on what I'm doing wrong?

My modal html is:

<!-- 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>
Jimmy Genslinger
  • 557
  • 3
  • 21

1 Answers1

0

if Response.End() Throws exception when using along with Response.Flush(). Replace Response.End with these lines

HttpContext.Current.Response.Flush();
HttpContext.Current.Response.SuppressContent = True;
HttpContext.Current.ApplicationInstance.CompleteRequest();
kumar chandraketu
  • 2,232
  • 2
  • 20
  • 25