This discussion is archived
8 Replies Latest reply: Jun 24, 2013 9:46 PM by Max L RSS

Trigger Event After File Download Completed

raudabat Newbie
Currently Being Moderated
Do anyone know if it is possible to trigger an event on Apex page after the completion of a file download event?

Thanks,

Tom
  • 1. Re: Trigger Event After File Download Completed
    VC Guru
    Currently Being Moderated
    raudabat wrote:
    Do anyone know if it is possible to trigger an event on Apex page after the completion of a file download event?

    Thanks,

    Tom
    Tom,

    I assume you are talking about triggering a dynamic action(or javascript code) after a file is downloaded? and there is nothing called download event

    But it is possible to achieve this but it depends on how the file is being rendered/downloaded i.e. is the file downloaded via a database procedure??

    See http://stackoverflow.com/questions/1106377/detect-when-browser-receives-file-download
    I did this some while ago and it works perfectly on all browsers, the approach I followed was:

    1. set a cookie when printing the file (pl/sql)
    owa_util.mime_header('text/html', FALSE);
    // Create a cookie
    owa_cookie.send( name=>'DOWNLOAD_STATUS', //some unique name
    value=>'Complete', expires=> sysdate+1, path=>'/');
    ....
    //PRINT blob
    ....
    2. In the apex wrap your download link to trigger a javascript function which runs every 1 sec to check the cookie value and finishes once the cookie value is Complete
    See apex.storage.setCookie and apex.storage.getCookie
    //this is wrapper function to download the file
    function downloadFile() {
     //set the cookie
     apex.storage.setCookie('DOWNLOAD_STATUS', 'In Progress');
     checkDownloadStatus();
    apex.submit({
         request : "DOWNLOAD_FILE",
         showWait : true
    });
    }
     
    function checkDownloadStatus() {
     console.log(1);
         if (apex.storage.getCookie('DOWNLOAD_STATUS') == 'Complete') {
              //download complete trigger your javascript event
                    $('#apex_wait_overlay,#apex_wait_popup').remove();
              return;
         } else {
                    //the download is not complete yet check again after 1 second
              setTimeout("checkDownloadStatus()", 1000);
         }
    }
    try this, I haven't tested this code so let me know if you find any issue.
  • 2. Re: Trigger Event After File Download Completed
    raudabat Newbie
    Currently Being Moderated
    VC,
    Thanks for the input!!!! I will try this and give you feedback. It seems like it should work and I will change the status. Also, thanks for the quick reply.

    Tom
  • 3. Re: Trigger Event After File Download Completed
    VC Guru
    Currently Being Moderated
    Yes it does see this working example http://apex.oracle.com/pls/apex/f?p=46417:49

    shows a waiting dialog and hiding it after the download is complete.

    Cheers,
    Vikram
  • 4. Re: Trigger Event After File Download Completed
    raudabat Newbie
    Currently Being Moderated
    VC,

    I see it working on the site. I tried the implementation and when I view my response header in firefox it looks like I am getting the same cookie with 2 values. It looks like it is not updating the same cookie from my PL/SQL send Cookie statement. Any ideas?

    Tom
  • 5. Re: Trigger Event After File Download Completed
    raudabat Newbie
    Currently Being Moderated
    VC,

    Finally got this to work. When I looked at my cookies in Firefox they had different paths "/" vs "/pls/apex/" (the one defined in apex javascript) plus one was secure and the other was not. Once I synced these values up the method worked.

    Thanks for the idea !!!!

    Tom
  • 6. Re: Trigger Event After File Download Completed
    Max L Newbie
    Currently Being Moderated

    Hi VC,

     

    I like this solution and I have a similar challenge. In the demo, does the file download PL/SQL code run from a Branch or from a Process?

     

    I have a stored procedure to create a blob. The stored procedure runs from a "On Submit - After Computations and Validations" process and it can take minutes to complete. The stored procedure uses WPG_DOCLOAD.DOWNLOAD_FILE and APEX_APPLICATION.STOP_APEX_ENGINE. The file download won't work without APEX_APPLICATION.STOP_APEX_ENGINE if the stored procedure runs from a process. But I think this APEX_APPLICATION.STOP_APEX_ENGINE also halts the client-side javascript code execution.

     

    Any recommendation?

     

    Max

  • 7. Re: Trigger Event After File Download Completed
    Max L Newbie
    Currently Being Moderated

    Hi VC,

     

    I like this solution and I have a similar challenge. In the demo, does the file download PL/SQL code run from a Branch or from a Process?

     

    I have a stored procedure to create a blob. The stored procedure runs from a "On Submit - After Computations and Validations" process and it can take minutes to complete. The stored procedure uses WPG_DOCLOAD.DOWNLOAD_FILE and APEX_APPLICATION.STOP_APEX_ENGINE. The file download won't work without APEX_APPLICATION.STOP_APEX_ENGINE if the stored procedure runs from a process. But I think this APEX_APPLICATION.STOP_APEX_ENGINE also halts the client-side javascript code execution.

     

    Any recommendation?

     

    Max

  • 8. Re: Trigger Event After File Download Completed
    Max L Newbie
    Currently Being Moderated

    Please disregard my previous posts. All is well. The javascript script codes just need to be defined at the page level.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points