我使用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();
}
希望天才能理解我的问题
答案 0 :(得分:1)
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();
});
答案 1 :(得分:0)
试试这个
window.location.replace('Your URL Here')
并在get中传递所需的变量。
看起来很正常,但效果很好。