简体   繁体   English

文件下载对话框何时打开? - HTML,Javascript

[英]When Does the File Download Dialog Open? - HTML, Javascript

I have a html page, and when I click a file link on the page, the file download dialog pops up, and this file dialog locks the page. 我有一个html页面,当我单击页面上的文件链接时,弹出文件下载对话框,此文件对话框将锁定页面。 I mean without selecting one of the choices(Open, Save, Cancel) I can't do anything on the page (which is normal). 我的意思是没有选择其中一个选项(打开,保存,取消)我在页面上无法做任何事情(这是正常的)。 What I need is if javascript can check whether the page is locked or not. 我需要的是,如果javascript可以检查页面是否被锁定。 (or whether the file dialog is popped?) (或者是否弹出文件对话框?)

ps don't say "put an onclick event to the link", because the server may respond very slowly (like 30 seconds after clicking) ps不要说“将一个onclick事件放到链接上”,因为服务器可能响应非常慢(例如点击后30秒)

If you know that your server is returning a file for download, then you can guess when the dialog comes up by this round-about trick (which I got from a stackoverflow answer to a related question I asked): 如果你知道你的服务器正在返回一个文件供下载,那么你可以猜到对话框是什么时候出现这个圆形技巧(我从stackoverflow回答我问到的相关问题):

  1. When you issue the HTTP request from your page, add a parameter whose value is some random string (like "random" + new Date().getTime() — it doesn't have to be secure but you want to avoid collisions) 当您从页面发出HTTP请求时,添加一个值为随机字符串的参数(如"random" + new Date().getTime() - 它不必是安全的,但您希望避免冲突)

  2. The server looks for that parameter. 服务器查找该参数。 When it starts sending back the attachment for download, it adds a cookie with your random string as its value. 当它开始发送回附件以供下载时,它会添加一个以随机字符串作为其值的cookie。

  3. Now, after the page issues the HTTP request, it starts an interval timer. 现在,在页面发出HTTP请求之后,它启动间隔计时器。 The code in the timer function checks document.cookie looking for that random string. 计时器功能中的代码检查document.cookie查找该随机字符串。 As soon as document.cookie contains that string, then you know the HTTP response has made it to the browser. 只要document.cookie包含该字符串,您就知道HTTP响应已经进入浏览器。 Since you know that the browser will ask the user to save the attachment, you can infer that the file dialog is being shown at that point. 由于您知道浏览器会要求用户保存附件,因此您可以推断出该文件对话框正在显示。

What I needed this for was a way to deal with the IE security thing about file attachments coming from events other than direct user "clicks". 我需要这个是一种处理IE安全事件的方法,该文件附件来自直接用户“点击”以外的事件。 I needed to close a modal dialog, so I needed to know when the HTTP response would return. 我需要关闭一个模态对话框,所以我需要知道HTTP响应何时返回。

Because the page is locked , you can't do anything with JavaScript, because it is locked as well. 由于页面已锁定 ,因此您无法对JavaScript执行任何操作,因为它也已锁定。

But, what are you trying to do? 但是,你想做什么? Are you trying to somehow log the fact that the user is downloading the file? 您是否试图以某种方式记录用户正在下载文件的事实? If yes, there are better ways to do it, and they're on the server-side. 如果是的话,有更好的方法,它们在服务器端。 Use some server-side scripting language to serve the file and log the fact that it was downloaded. 使用某种服务器端脚本语言来提供文件并记录下载它的事实。

If that's not what you're trying to do, then the only way is using either onclick on the link or onunload / onbeforeunload , but these are less reliable and I am sure that you will find completely different behavior on different browsers. 如果那不是你想要做的,那么唯一的方法就是在链接或onunload / onbeforeunload上使用onclick ,但这些不太可靠,我相信你会在不同的浏览器上找到完全不同的行为。

Actually, now that I think of it, there is one more way, but it's very dirty . 实际上,现在我想起来了,还有一种方法,但它非常脏 The idea is to set an interval to run every second and check if between two runs more than a second has passed. 我们的想法是设置一个每秒运行一次的间隔,并检查两次运行之间是否超过一秒。 Something like: 就像是:

var lastTime = new Date().getTime();
function checkTime() {
    var curTime = new Date().getTime();
    if (curTime - lastTime > 1100) { // 1100 because there might be small browser lags 
        // do something after the dialog appeared and the user did something with it
    }
    lastTime = curTime;
}
setInterval(checkTime, 1000);

Please note, that there are browsers (Chrome is an example, I think) that do not block the page while that dialog is opened, so this might not work. 请注意,有些浏览器(我认为Chrome就是一个例子)在打开对话框时不会阻止页面,所以这可能不起作用。 Make sure to double-cross-check everything if you go about using this. 如果你要使用它,请务必双重交叉检查所有内容。

I have to go take a shower now. 我现在得去洗个澡。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM