自动关闭带有要下载文件的弹出窗口 [英] Auto-closing a popup with a file to download

查看:423
本文介绍了自动关闭带有要下载文件的弹出窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为download.php的文件,其中包含要下载的文件,因此它具有以下标头(用PHP声明):

I have a file, named download.php, which contains a file to be downloaded, so it has the following headers (declared with PHP):

header("Content-Type: ".pathinfo($_GET['file'], PATHINFO_EXTENSION));
header("Content-Length: ". filesize($_GET['file']));
header("Content-Disposition: attachment; filename=". $_GET['file']);

download.php作为其他页面的jQuery弹出窗口.

download.php is opened as a popup with jQuery by an other page.

现在,我希望download.php在几秒钟后自动关闭(使用JavaScript)(因此,我确信下载开始了),但是我没有写出有效的代码. 这是我尝试过的代码(我将它们放在 标头之后):

Now I want download.php to self-close (using JavaScript) after some seconds (so I'm secure that download started) but I didn't manage to write working code. Here are the codes I tried (I placed them after headers):

window.setTimeout('self.close();', 3000);
window.setTimeout('function(){self.close();}', 3000);
window.setTimeout(self.close();, 3000);

我也简单地尝试过:

self.close();

但它仍然无法正常工作.

but it does not work anyway.

我试图将这些代码同时放入<head><body>.

I tried to put these codes both in <head> and in <body>.

可能是什么问题?

推荐答案

能否在此打开的窗口的浏览器网址栏中询问它的内容.在某些情况下,浏览器会看到标头,让浏览器知道将其视为下载内容,而不会将窗口作为真实页面运行.而是打开"about:blank"之类的内容.如果是这种情况,页面上的javascript将永远无法运行.

Can I ask what it says in the browser url bar in this opened window. It might be the case that the browser see's the headers letting the browser know it is to be treated as a download and doesn't run the window as a true page. and instead opens something like 'about:blank'. If that's the case the on the page javascript would never get run.

但是我可以提出以下建议.我假设此窗口正在由另一个页面打开.在这种情况下,让另一页通过javascript以编程方式打开窗口,然后从那里控制关闭.

I can suggest the following however. I'm assuming this window is being opened by another page. In that case have the other page open the window programatically through javascript and control the close from there.

var popout = window.open("http://example.com/download.php");
window.setTimeout(function(){
    popout.close();
}, 1000);

这篇关于自动关闭带有要下载文件的弹出窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆