我们有下面的代码
Javascript:
function Download() {
//Show Loading Spinner
location.href = "FileDownloadHandler.ashx";
//Some Code(Hiding Loading Spinner)
}
aspx页面:
<input type="button" value="Download" onclick="Download();" />
“FileDownloadHandler”将文件下载给用户。
实际结果:
location.href下的代码正在立即执行,而未完成处理程序的执行。
预期结果:
我想停止执行“SomeCode”,直到处理程序完成执行。
我该如何实现?
提前致谢。
最佳答案
最后,我找到了解决问题的方法。使用jQuery插件,我们可以实现这一目标。我已经实现了它,并且像一种魅力一样工作。
此插件可以下载文件,并且具有成功和失败的回调,因此我们可以控制应在文件下载完成后执行的代码。
在这里,而不是location.href,我们必须调用此插件来调用FileDownloadHandler,如下所示。
$("#loading").show();
$.fileDownload('FileDownloadHandler.ashx', {
successCallback: function (url) {
$("#loading").hide();
},
failCallback: function (html, url) {
$("#loading").hide();
alert("Failed");
}
});
下面有更多详细信息
http://johnculviner.com/jquery-file-download-plugin-for-ajax-like-feature-rich-file-downloads/
http://jqueryfiledownload.apphb.com/
http://github.com/johnculviner/jquery.fileDownload/blob/master/src/Scripts/jquery.fileDownload.js