我正在使用 codeigniter 和 ajax 下载文件。
我的 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();
}
希望天才能理解我的问题