Detect when browser receives file download

前端 未结 22 1739
陌清茗
陌清茗 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:41

    i use the following to download blobs and revoke the object-url after the download. it works in chrome and firefox!

    function download(blob){
        var url = URL.createObjectURL(blob);
        console.log('create ' + url);
    
        window.addEventListener('focus', window_focus, false);
        function window_focus(){
            window.removeEventListener('focus', window_focus, false);                   
            URL.revokeObjectURL(url);
            console.log('revoke ' + url);
        }
        location.href = url;
    }
    

    after the file download dialog is closed, the window gets her focus back so the focus event is triggered.

提交回复
热议问题