使用 AJAX/MVC 3 下载 PDF 文件
基本上问题是这样的:
我有一个 pdf 文件,我根据当前登录用户的 ID 和 OID 从服务器请求该文件。为了获取 OID,我必须根据用户单击的按钮从 HTML 表中解析一个值,然后将其发送到家庭控制器。然后运行 API 调用来获取 OID。然后使用 OID 进行另一个 API 调用以提取 PDF 文件。 AJAX 用于将解析后的 HTML 值发送到家庭控制器,使用以下代码:
$('.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
});
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 API 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
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,不要使用 AJAX 下载文件。只需将
pReference
作为查询字符串参数传递即可在 JavaScript 中进行重定向:Yes, don't use AJAX to download files. Simply redirect in javascript by passing the
pReference
as query string parameter: