如何在下载文件后执行一些JavaScript? [英] How do I execute some javascript after a file is downloaded?

查看:81
本文介绍了如何在下载文件后执行一些JavaScript?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个链接到文件的页面。点击链接后,我使用下面的代码显示加载消息:

  $('#TerritoriesToExcelLink')。click函数(){
$('#TerritoriesToExcelLoading')。show();
window.location.href = $(this).attr('href');
});

我想在文件下载完成后隐藏信息,保存对话框弹出浏览器。



我尝试添加一些在 ready()上触发的代码,但似乎只是直线运行(大概是因为即使文件不存在,页面也已经被加载),所以加载消息从不显示。



我怎样才能隐藏加载消息一旦文件被完全下载后,你的服务器发送一个随机的cookie,你从客户端指定的在HTTP标头中下载您的代码。在您的Javascript中进行投票以检查cookie的存在。这应该告诉你什么时候浏览器有你的文件。


I have a page with a link to a file. When the link is clicked I use the code below to show a loading message:

$('#TerritoriesToExcelLink').click(function() {
    $('#TerritoriesToExcelLoading').show();
    window.location.href = $(this).attr('href');
});

I'd like to hide the message once the file is downloaded and the save dialog pops up in the browser.

I've tried adding some code that fires on ready() but that seems to just run straight away (presumably since the page is already loaded even if the file isn't) so the loading message never gets displayed.

How can I hide the loading message once the file has been completely downloaded?

解决方案

Have your server send a random cookie that you specify from your client-side code with your download in the HTTP headers. Poll in your Javascript to check for the presence of the cookie. This should tell you when the browser has your file.

这篇关于如何在下载文件后执行一些JavaScript?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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