使用codeigniter和Ajax下载文件时遇到问题


having trouble in downloading file using codeigniter and Ajax

我正在使用codeigniter和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)
    {}
    });
    });

它正在调用我在控制器中的 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和PHP性能完美,并在pdfcache文件夹中创建了PDF,问题是创建文件时没有下载文件。

这是我的下载脚本

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

希望你天才理解我的问题

application/force-download不是MIME类型。

尝试

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

试试这个

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

并在 get 中传递所需的变量。

它看起来很正常,但效果很好。