I'm trying to implement a function that on Click event, Download a file, and close the UI dialog When the file download is finished.
The problem is the, after $preparingFileModal.dialog({ modal: true }) the code doesn't trigger anymore and successCallback cannot detect the end fo file download .
$(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>
Have a look at Jquery File download ($.fileDownload)
You need to set header "Set-Cookie: fileDownload=true; path=/"
header("Set-Cookie: fileDownload=true; path=/"); is how I did it in PHP. I start zipping some files when the user clicks on the download button. After the zip file is created, I set the header as above and echo the zip file path to the browser and start the download via jqueryFileDownload.
//set filedownload cookie.
header('Set-Cookie: fileDownload=true; path=/');
echo json_encode(array("OK" => "Zip file created", 'file' => $zipFileName));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With