使用Ajax下载JQuery文件 [英] JQuery file download with Ajax

查看:90
本文介绍了使用Ajax下载JQuery文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我的用户选择生成报告时,我正在使用John Culviner的出色的fileDownload插件生成请稍候"消息.

I'm using John Culviner's great fileDownload plugin to produce a "please wait" message when my user chooses to generate a report.

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

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

这时,当我单击链接时,模态将正确显示,并带有请稍候"消息.我的文件确实建立在服务器上(通过成功处理程序中的警报确认),并且我的HREF确实得到了更新.但是,该插件不会提示用户下载.

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.

谢谢!

推荐答案

可能是您需要开始下载,在收到链接参考后:

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;
});

这篇关于使用Ajax下载JQuery文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆