Search code examples
jqueryasp.net-mvcdommime-typesjquery-events

jQuery event after a PDF loads?


I have a modal dialog that allows users to generate a customized PDF. After they click a "Start" button, the page redirects with window.location to a controller, which then creates the PDF and returns it.

Because of how long it takes the PDF to return, I want to display a modal "Generating PDF" dialog that automatically closes once the PDF is complete and is returned.

The problem is I can't figure out how/when to execute that final function to close the "Generating PDF" dialog. Since the controller returns an application/pdf type, there is no document object event to attach to, right? What is the best way to do this?


Solution

  • Thank you for your responses. @PGill was on the right track. I've simplified his approach:

    1. User clicks the Start button
    2. The "Generating PDF" modal dialog is displayed
    3. An AJAX request is sent to Controller 1 to generate the PDF
    4. Controller 1 creates the PDF (which takes 5-60 seconds), stores it in a secure location, and returns a unique key
    5. The "Generating PDF" modal dialog is hidden
    6. The browser is redirected to Controller 2 and sent the unique key
    7. Controller 2 verifies the key, retrieves the PDF from the secure location, and returns it

    The solution here was to separate the Generation of the PDF from the Download of the PDF into 2 steps, so that between them, the modal dialog can be hidden.

    which verifies it, retrieves the PDF rom the secure location and returns it.

    ShowModalDialog();
    
    $.ajax({
      type: "GET",
      url: 'GeneratePDFReportForDownload?' + params,
      success: function (pdfkey, textStatus, jqXHR) {
          HideModalDialog();
          window.location = 'DownloadPDFReport?key=' + pdfkey;
      },
      async: true,
      datatype: 'json'
    });