使用 AJAX/MVC 3 下载 PDF 文件

发布于 2024-12-06 19:05:57 字数 1380 浏览 1 评论 0原文

基本上问题是这样的:

我有一个 pdf 文件,我根据当前登录用户的 ID 和 OID 从服务器请求该文件。为了获取 OID,我必须根据用户单击的按钮从 HTML 表中解析一个值,然后将其发送到家庭控制器。然后运行 ​​API 调用来获取 OID。然后使用 OID 进行另一个 API 调用以提取 PDF 文件。 AJAX 用于将解析后的 H​​TML 值发送到家庭控制器,使用以下代码:

        $('.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技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

望笑 2024-12-13 19:05:57

是的,不要使用 AJAX 下载文件。只需将 pReference 作为查询字符串参数传递即可在 JavaScript 中进行重定向:

window.location.href = '@Url.Action("PrintFromGrid", "Home")?pReference=' + encodeURIComponent(primaryReference);

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文