简体   繁体   English

使用codeigniter和Ajax下载文件时遇到麻烦

[英]having trouble in downloading file using codeigniter and Ajax

i am using codeigniter and ajax for downloading a file. 我正在使用codeigniter和ajax下载文件。

My Ajax Request 我的Ajax请求

    $('.exporter').bind('click',function()
    {
    var callfunction = 'reportprints/print_'+$(this).attr('id').toLowerCase();
    var Query = $('#exportQuery').val();                        
    $.ajax({
    url: callfunction,
    type: 'POST',           
    dataType: 'html',
    data:{q:Query},                                                         
    success: function(output)
    {}
    });
    });

it's calling my PDF Function which are in Controller 它正在调用Controller中的我的PDF函数

    public function print_pdf()
    {
        $Query = $this->input->post('q');
        // set document information
        $this->pdf->SetAuthor('PrimexOne Exchange');
        $filename = $this->makefilename('pdf','DSP');
        $this->pdf->SetTitle('Daily Sales Report - '.date('d/m/Y'));
        $this->pdf->SetSubject('PrimexOne Sales Report');
        $this->pdf->SetKeywords('keywords');        
        $this->pdf->SetFont('helvetica', 'N', 12);   
        // add a page
        //$this->pdf->AddPage();     
        // write html on PDF
        $this->pdf->Cell(0,10,'Welcome in PrimexOne');

        //Creating FileName

        $filepath = APPPATH.'cache/pdfcache/';
        $fullname = $filepath.$filename;
        $this->pdf->Output($fullname, 'F');     
        $this->downloadfile(array('format'=>'pdf','filename'=>$filename,'filepath'=>$fullname));            
    }

ajax and PHP perform perfectly and created PDF in pdfcache folder, problem is it's not downloading files when file created. ajax和PHP表现完美,并且在pdfcache文件夹中创建了PDF,问题是创建文件时没有下载文件。

here is my downloading script 这是我的下载脚本

    public function downloadfile($arr)
    {

         $mime = 'application/force-download';
           header('Pragma: public');    
           header('Expires: 0');        
           header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
           header('Cache-Control: private',false);
           header('Content-Type: '.$mime);
           header('Content-Disposition: attachment; filename="'.$arr['filename'].'"');
           header('Content-Transfer-Encoding: binary');
           header('Connection: close');
           readfile($arr['filepath']);    
           exit();

    }   

hope you Genius understand my problem 希望你天才理解我的问题

application/force-download is not a mime type. application/force-download不是mime类型。

Try 尝试

$mime = 'application/pdf';

$('.exporter').bind('click',function(){
    var Query = $('#exportQuery').val();
    var callfunction = 'reportprints/print_'+$(this).attr('id').toLowerCase();
    event.preventDefault();
    var newForm = $('<form>', {
        'action': callfunction,
        'target': '_top'
    }).append($('<input>', {
        'name': 'q',
        'value': Query,
        'type': 'hidden'
    }));
    newForm.submit();
});

Try This 尝试这个

window.location.replace('Your URL Here')

And pass the required variables in get. 并在get中传递所需的变量。

It will look like normal but works perfectly. 看起来很正常,但效果很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM