Detect when browser receives file download

前端 未结 22 1742
陌清茗
陌清茗 2020-11-21 04:55

I have a page that allows the user to download a dynamically-generated file. It takes a long time to generate, so I\'d like to show a \"waiting\" indicator. The problem is,

22条回答
  •  滥情空心
    2020-11-21 05:25

    Based on Elmer's example I've prepared my own solution. After elements click with defined download class it lets to show custom message on the screen. I've used focus trigger to hide the message.

    JavaScript

    $(function(){$('.download').click(function() { ShowDownloadMessage(); }); })
    
    function ShowDownloadMessage()
    {
         $('#message-text').text('your report is creating, please wait...');
         $('#message').show();
         window.addEventListener('focus', HideDownloadMessage, false);
    }
    
    function HideDownloadMessage(){
        window.removeEventListener('focus', HideDownloadMessage, false);                   
        $('#message').hide();
    }
    

    HTML

    
    

    Now you should implement any element to download:

    Download report
    

    or

    
    

    After each download click you will see message your report is creating, please wait...

提交回复
热议问题