donglaohua1671 2016-11-11 13:03
浏览 85
已采纳

使用CodeIgniter和PHPExcel从ajax响应处理文件下载的任何方法

I am struggling with achieving what I want and am not sure if it is possible. I am using CodeIgniter and PHP for my web application. My home view allows users to query a database and the results are generated and displayed in the table form on the page. The user will then review the table and be provided the option of downloading as an xlsx Excel file. I am using an ajax post to send the table data to my PHP controller that will create the Excel file. I also need this to work in all browsers especially Safari, as the client will access the application from Ipads as well as desktop machines.

My home ajax post: (t is the id of the generated table)

$("#btnExport").click(function() {
    $.ajax({
        type: "POST",
        url: '<?php echo base_url();?>index.php/Home/excel',
        datatype: 'html',
        data: {
            'queryData': $("#t").html()},
        success: function (response) {
            alert(response);
        }             
    });
});

My controller function:

public function excel()
{
    $this->load->library('excel');
    $filename = "data";
    $table = $this->input->post('queryData');
    $objPHPExcel = new PHPExcel();
    $tmpfile = time().'.html';
    file_put_contents($tmpfile,$table);
    $objReader = PHPExcel_IOFactory::createReader('HTML');
    $objPHPExcel = $objReader->load($tmpfile);

    header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
    header('Content-Disposition: attachment;filename='.$filename);
    header('Cache-Control: max-age=0');

    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
    $objWriter->save('php://output');       
    unlink($tmpfile);
}

The problem is the handling of the ajax response I've read its impossible to download a file from ajax. Everything I have tried is not working The response is binary encoded data I am assuming is the excel file. Is there a workaround to this problem? I decided on ajax as the table can change dynamically given the current query, and needs to be sent to the server accordingly.Thanks for any assistance.

EDIT In the linked question it is about submitting forms and not triggering a download. I am not submitting any forms, just passing an html table to the PHP to build the Excel file. I fail to see how that is relevant to my problem. Am I misunderstanding?

  • 写回答

2条回答 默认 最新

  • dongliao2011 2016-11-12 11:57
    关注

    Submitting a form is the best you can do to trigger a file download... It's better to create one even if you don't have a form.

    var baseUrl = '<?php echo base_url();?>'
    
    var $form = $(`<form method="post" hidden action="${baseUrl}index.php/Home/excel"><textarea name="queryData">`)
    $form.find('textarea').val($("#t").html())
    $form.appendTo('body').submit()
    

    There is however one alternative to saving ajax responses and that is by creating a link with a object url from a blob response, setting the download attribute and trigger a click (in IE you need to call navigator.saveOrOpenBlob)
    There is a good lib out there that is called FileSaver to abstract out all nasty vendor specifics

    The way you would use it is by doing this:

    fetch(url, {method: 'post', body: data})
    .then(res => {
       var cd = res.header.get('Content-Disposition')
       var filename = cd.match(/filename="(.+)"/)[1]
    
       return res.blob().then(blob => saveAs(blob, filename))
    })
    

    But it's not as good as submitting a form and getting a bit of help from a server that can send a Content-Disposition header cuz not all browser (safari mainly) supports download attribute in links...

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀