加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

使用Ajax下载JQuery文件

发布时间:2020-12-16 02:56:59 所属栏目:百科 来源:网络整理
导读:当我的用户选择生成报告时,我正在使用John Culviner的文件下载插件来生成“请稍候”消息. 当用户单击链接时,我向我的PHP发送ajax请求,该请求在服务器上生成PDF.那时我正在尝试更新fileDownload插件的成功处理程序中的链接. 我可能正在接近这个错误,但这是我
当我的用户选择生成报告时,我正在使用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:

<div id="preparing-file-modal" title="Preparing report..." style="display: none;">
    We are preparing your report,please wait...

    <div class="ui-progressbar-value ui-corner-left ui-corner-right" style="width: 100%; height:22px; margin-top: 20px;"></div>
</div>

<div id="error-modal" title="Error" style="display: none;">
    There was a problem generating your report,please try again.
</div>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读