Detect when browser receives file download

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

    If Xmlhttprequest with blob is not an option then you can open your file in new window and check if eny elements get populated in that window body with interval.

    var form = document.getElementById("frmDownlaod");
     form.setAttribute("action","downoad/url");
     form.setAttribute("target","downlaod");
     var exportwindow = window.open("", "downlaod", "width=800,height=600,resizable=yes");
     form.submit();
    
    var responseInterval = setInterval(function(){
    	var winBody = exportwindow.document.body
    	if(winBody.hasChildNodes()) // or 'downoad/url' === exportwindow.document.location.href
    	{
    		clearInterval(responseInterval);
    		// do your work
    		// if there is error page configured your application for failed requests, check for those dom elemets 
    	}
    }, 1000)
    //Better if you specify maximun no of intervals

提交回复
热议问题