Javascript Chrome 获取下载文件时会触发哪个 JS 事件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6126619/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Which JS event is fired when Chrome gets the download file?
提问by ?inh H?ng Chau
I am having a problem with onLoad event of an iframe on Google Chrome. I created an iframe and set value for its "src" attribute to get a file from server. While server is processing, a waiting box is displayed until client gets the returned file. I tried to use the onLoad event of iframe to detect when client get the file to turn off that waiting box, but on Google Chrome that event handler does not work.
With Firefox, when client gets a file, a "Save to" popup will be displayed automatically and event "load" will be fired, but this is not happen on Chrome.
Could you please tell me how to handle this issue? Thank you so much!
我在 Google Chrome 上遇到 iframe 的 onLoad 事件问题。我创建了一个 iframe 并为其“src”属性设置值以从服务器获取文件。服务器正在处理时,会显示一个等待框,直到客户端获取返回的文件。我尝试使用 iframe 的 onLoad 事件来检测客户端何时获取文件以关闭该等待框,但在 Google Chrome 上该事件处理程序不起作用。
使用 Firefox,当客户端获取文件时,将自动显示“保存到”弹出窗口并触发事件“加载”,但在 Chrome 上不会发生这种情况。
你能告诉我如何处理这个问题吗?非常感谢!
采纳答案by Dan
I've run into this exact issue. It turns out Chrome triggers absolutely no events in an iframe upon a file download.
我遇到了这个确切的问题。事实证明,Chrome 在下载文件时绝对不会在 iframe 中触发任何事件。
Since there are no events to look out for and you're returning a file (as apposed to any inline content) the workaround I was forced to inspect the contents of the iframe after a few seconds, if it is empty then assume there were no errors and the results were processed correctly. If it contains data (my server will return JSON if there are any errors) then handle the error data accordingly.
由于没有要查找的事件并且您正在返回一个文件(与任何内联内容相关),因此我被迫在几秒钟后检查 iframe 的内容,如果它为空,则假设没有错误并且结果被正确处理。如果它包含数据(如果有任何错误,我的服务器将返回 JSON)然后相应地处理错误数据。
回答by Andrea
I've run into this same problem and I ended up using this plugin: http://johnculviner.com/jquery-file-download-plugin-for-ajax-like-feature-rich-file-downloads/
我遇到了同样的问题,我最终使用了这个插件:http: //johnculviner.com/jquery-file-download-plugin-for-ajax-like-feature-rich-file-downloads/
It's based on the same concept morgan describes, it's easy to use and and it worked great.
它基于摩根描述的相同概念,它易于使用并且效果很好。
回答by morgan
The way I got around this issue was to set a cookie (done = 0) when the page that is going to have the iframe loads, I use javascript to drop the iframe onto the page and I change the value of this cookie when the iframe's source script runs successfully (done = 1).
我解决这个问题的方法是在加载 iframe 的页面时设置一个 cookie (done = 0),我使用 javascript 将 iframe 放到页面上,并在 iframe 加载时更改此 cookie 的值源脚本成功运行(完成 = 1)。
I then used a simple ajax call with a settimeout to return the value of the cookie, when the value is 1 I can trigger the onload event (that works for all other browsers).
然后我使用一个带有 settimeout 的简单 ajax 调用来返回 cookie 的值,当值为 1 时,我可以触发 onload 事件(适用于所有其他浏览器)。