Downloading PDF File with AJAX/MVC 3
Basically the issue is this:
I have a pdf file that I am requesting from a server based on the currently logged in user's ID and the OID. In order to get the OID, I have to parse a value from a HTML table based on the button the user clicks on and then send it to the home controller. Then an API call is run to grab the OID. Another AP开发者_StackOverflowI call is then made with the OID to pull the PDF file. AJAX is used to send the the parsed HTML value to the home controller using this code here:
$('.pdfPrint').live('click', function () {
$(this).addClass('selectedDetails');
var parent = $('.selectedDetails').parents('tr');
var tr = $(this).parents('tr');
var td = tr.children();
var i = 0;
td.each(function () {
$(this).addClass('tdGrid' + i);
i++;
});
var primaryReference = "";
primaryReference = $('.tdGrid1').text();
gridClassRemover();
$.ajax({ // create an AJAX call...
data: { pReference: primaryReference }, // get the form data
type: "POST", // GET or POST
url: "/Home/PrintFromGrid", // the file to call
success: function (response) { // on success..
callTrackDialog();
// update the DIV
}, // end of success
error: function () {
alert('An error has occurred. If this problem persists please contact support.');
}
}); // end of .ajax
});
Yes, don't use AJAX to download files. Simply redirect in javascript by passing the pReference
as query string parameter:
window.location.href = '@Url.Action("PrintFromGrid", "Home")?pReference=' + encodeURIComponent(primaryReference);
精彩评论