I'm using John Culviner's great fileDownload plugin to produce a "please wait" message when my user chooses to generate a report.
When a user clicks a link, I send an ajax request to my PHP which generates a PDF on the server. At that point I am attempting to update the link in my success handler for the fileDownload plugin.
I may be approaching this wrong, but here's my code - any help is greatly appreciated.
$("body").on("click","##pdfLink", function(e){
$.fileDownload($(this).attr('href'), {
preparingMessageHtml: "Preparing your report, please wait...",
failMessageHtml: "There was a problem generating your report, please try again."
});
// Build our data string.
var strData = {method: "createPDF"};
// Send a request to build our XL file.
$.ajax({
url: '/pdf.php',
data: strData,
type: 'get',
dataType: 'json',
success: function(data) {
alert(data);
$("##pdfLink").attr("href","/pdf/"+data);
},
error: function(e) {
console.log(e);
}
});
return false;
e.preventDefault();
})
At this point, when I click the link the modal is displayed correctly with the "Please Wait" message. My file does get built on the server (Confirmed with my alert in my success handler), and my HREF does get updated. However, the plugin does not prompt the user for download.
Thanks!
You not need call ajax funcion in JS. Your link <a href='yoursite.com/pdf.php'
identify with this command $(this).attr('href')
location of you server process of pdf.
EDIT:
Your call:
// Build our data string.
var strData = {method: "createPDF"};
var $preparingFileModal = $("#preparing-file-modal");
$preparingFileModal.dialog({ modal: true });
$.fileDownload($(this).attr('href'), {
httpMethod: "GET",
data: strData
successCallback: function (responseHtml, url) {
$preparingFileModal.dialog('close');
// In this case
$.fileDownload("/pdf/"+responseHtml, {
preparingMessageHtml: "Download file",
failMessageHtml: "Not work"
});
},
failCallback: function (responseHtml, url) {
$preparingFileModal.dialog('close');
$("#error-modal").dialog({ modal: true });
}
});
HTML:
<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>
Probably , you need to start download ,after link's reference will be received:
$("body").on("click","##pdfLink", function(e){
// Build our data string.
var strData = {method: "createPDF"};
var self = this;
var $pdfGeneratingDialog = $('<div>',{
title:"Generating PDF",
text: "Please wait..."
}).dialog({modal:true});
// Send a request to build our XL file.
$.ajax({
url: '/pdf.php',
data: strData,
type: 'get',
dataType: 'json',
success: function(data) {
$pdfGeneratingDialog.dialog('close');
alert(data);
$(self).attr("href","/pdf/"+data);
//starting download process
$.fileDownload($(self).attr('href'), {
preparingMessageHtml: "Preparing your report, please wait...",
failMessageHtml: "There was a problem generating your report, please try again."
});
},
error: function(e) {
console.log(e);
}
});
return false;
});
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