我即时创建了一个 Iframe,并将下载二进制文件(xls、doc ...)的页面设置为 url 。在下载文件时,我会播放动画。什么时候没有,我隐藏它。
问题是 Chrome 不知道文件何时完全下载,即 iframe 何时完全加载。我使用 iframe 属性readyState来检查 iframe 状态:
var iframe = document.createElement("iframe");
iframe.style.visibility = "hidden";
// I start a progress animation
window.setTimeout(showProgressAnimation, 1000);
// I start the file download
iframe.src ='GetFile.aspx?file=' + fileName;
document.body.appendChild(iframe);
function showProgressAnimation() {
   if (iframe.readyState == "complete" || iframe.readyState == "interactive") {
      // I stop the animation and show the page
      animation.style.display = 'none';
      progressBar.hide();
      $('#page').show();
   }
   else {
      // Chrome is always getting into this line
      window.setTimeout(showProgressAnimation, 1000);
   }
}
所以结果是无限循环。
我尝试了以下方法,它在 Firefox 和 Chrome 中有效,但在内容是二进制文件时无效:
if ($.browser.mozilla || $.browser.webkit ) {
    iframe.onload = function showProgressAnimation() {
        animation.style.display = 'none';
        progressBar.hide();
        $('#page').show();
    }
}
// IE
else{
     window.setTimeout(showProgressAnimation, 1000);
}