我正在尝试实现一个功能,在单击事件时下载文件,并在文件下载完成时关闭 UI 对话框。
问题是,在 $preparingFileModal.dialog({ modal: true })
之后,代码不再触发,并且 successCallback
无法检测到文件下载结束。
$(function () {
$(document).on("click", "a.fileDownloadCustomRichExperience", function () {
var $preparingFileModal = $("#preparing-file-modal");
$preparingFileModal.dialog({ modal: true });
$.fileDownload($(this).prop('href'), {
successCallback: function (url) {
$preparingFileModal.dialog('close');
},
failCallback: function (responseHtml, url) {
$preparingFileModal.dialog('close');
$("#error-modal").dialog({ modal: true });
}
});
return false; //this is critical to stop the click event which will trigger a normal file download!
});
});
<div id="preparing-file-modal" title="Preparing report..." style="display: none;">
We are preparing your report, please wait...
<div class="ui-progressbar-value ui-corner-left ui-corner-right" style="width: 100%; height:22px; margin-top: 20px;"></div>
</div>
<div id="error-modal" title="Error" style="display: none;">
There was a problem generating your report, please try again.
</div>
最佳答案
看看Jquery File download ($.fileDownload)
您需要设置 header “Set-Cookie: fileDownload=true; path=/”
header("Set-Cookie: fileDownload=true; path=/");
是我在 PHP 中的做法。当用户单击下载按钮时,我开始压缩一些文件。创建 zip 文件后,我按上述方式设置 header ,并将 zip 文件路径回显到浏览器,然后通过 jqueryFileDownload 开始下载。
//set filedownload cookie.
header('Set-Cookie: fileDownload=true; path=/');
echo json_encode(array("OK" => "Zip file created", 'file' => $zipFileName));
关于javascript - Jquery FileDownload 不触发 successCallback 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26177265/