[英]Iframe.readyState does not work in chrome
我即时创建了一个 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);
}
您可以使用onload
来通知iframe
的负载
这是一个简单的例子,可以工作
var iframe = document.createElement("iframe");
iframe.style.display = "none";
// this function will called when the iframe loaded
iframe.onload = function (){
iframe.style.display = "block";
alert("loaded");
};
// set the src last.
iframe.src ='http://www.test.com';
// add it to the page.
document.getElementById("one").appendChild(iframe);
在这里测试:
http://jsfiddle.net/48MQW/5/
最后加载src
。
http://jsfiddle.net/48MQW/24/
可下载的文件内容不会触发 readystatechange 事件处理程序或 onload 事件处理程序。 这个原因你可以在服务器端设置一个 cookie 和文件内容一起,客户端会定期检查这个 cookie。 例如:
服务器
response.cookie('fileDownloaded','true');
response.header('attachment','your-file-name.any');
//...write bytes to response...
客户
var checker = setInterval(()=>{
if(document.cookie.indexOf('fileDownloaded')>-1){
alert('done');
clearInterval(checker);
}
},100);
当然,您可以使用您的框架正确检查 cookie 值,这只是一个 poc,而不是安全的 cookie 解析器。
请试试这个——你真的是在一行一行地混合 dom 和 jQuery
var tId;
function stopAnim() {
// I stop the animation and show the page
animation.hide();
progressBar.hide();
$('#page').show();
clearInterval(tId);
}
var iframe = $("<iframe />");
iframe.css("visibility","hidden");
iframe.on("readystatechange",function() {
if (this.readyState == "complete" || this.readyState == "interactive") {
stopAnim();
}
});
iframe.on("load",function() { // can possibly be deleted
if (tId) {
stopAnim();
}
});
iframe.attr("src","GetFile.aspx?file=" + fileName);
$("body").append(iframe);
tId = setInterval(function() {
// update progress here
}, 1000); //
Sarkiroka 的解决方案对我有用。 我在 cookie 名称中添加了一个实例id
以提供线程安全的解决方案:
const instanceId = "some uuid retrieved from client";
response.cookie(`fileDownloaded-${instanceId}`,'true', { path: '/', secure: true, maxAge: 90000});
response.header('attachment','your-file-name.any');
//...write bytes to response...
客户
var checker = setInterval(()=>{
if(document.cookie.indexOf(`fileDownloaded-${instanceId}`)>-1){
alert('done');
clearInterval(checker);
}
},100);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.