我有一个Web应用程序,用户可以通过单击按钮导出结果。据我所知,无法从javascript启动文件“下载”,所以我使用简单的服务器往返开始下载:
由于上传数据可能需要一些时间,因此我在步骤1之后将导出按钮上的文本更改为“准备下载”。在步骤4或5的某处,我想将按钮更改回正常状态。有没有办法知道浏览器何时开始接收来自帖子请求的响应?
答案 0 :(得分:0)
我尝试使用此解决方案:
这适用于Firefox,但当服务器响应是文件时,Chrome中不会触发onLoad事件。
我选择的解决方案使用cookie并适用于所有浏览器:
这是下载按钮的事件处理程序中的相关代码,其中#nonce
是我放置随机值的隐藏字段:
var nonce = Math.random(); // generate random number
$("#nonce").val(nonce); // set number as field value
$("#downloadDataset").button('loading'); // set button to loading state
var downloadTimer = setInterval(function () { // start polling the cookies
if (document.cookie.indexOf(nonce) != -1) {
$("#downloadDataset").button('reset'); // reset button
clearInterval(downloadTimer); // stop polling the cookies
}
}, 1000); // check every 1000ms