微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用Ajax下载JQuery文件

当我的用户选择生成报告时,我正在使用John Culviner的文件下载插件生成“请稍候”消息.

用户单击链接时,我向我的PHP发送ajax请求,该请求在服务器上生成PDF.那时我正在尝试更新fileDownload插件的成功处理程序中的链接.

我可能正在接近这个错误,但这是我的代码 – 非常感谢任何帮助.

$("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(); 
})

此时,当我单击链接时,模式会正确显示“请等待”消息.我的文件确实构建在服务器上(在我的成功处理程序中通过我的警报确认),我的HREF确实得到了更新.但是,该插件不会提示用户下载.

谢谢!

最佳答案
你不需要在JS中调用ajax函数.您的链接< a href ='yoursite.com / pdf.PHP'标识此命令$(this).attr('href')您的pdf服务器进程的位置. 编辑: 你的来电:

// 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:

display: none;"> We are preparing your report,please wait...
display: none;"> There was a problem generating your report,please try again.

原文地址:https://www.jb51.cc/jquery/428089.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐