その場で Iframe を作成し、バイナリ ファイル (xls、doc...) をダウンロードするページを URL として設定します。ファイルのダウンロード中にアニメーションを表示します。そうでないときは非表示にします。
問題は、ファイルが完全にダウンロードされたとき、つまり iframe が完全にロードされたときを Chrome が認識できないことです。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);
}