2013-09-05 2 views
1

Я использую codeigniter и ajax для загрузки файла.возникают проблемы при загрузке файла с использованием 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 выполнять отлично и созданный PDF в папке pdfcache, проблема в том, что это не скачивая файлы, когда файл создано.

вот мой сценарий загрузки

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

    } 

надеюсь, что вы понимаете Genius моя проблема

+1

Возможный дубликат [Загрузить файл через ajax call php] (http://stackoverflow.com/questions/6668776/download-file-through-an-ajax-call-php) – stormdrain

ответ

1

application/force-download не тип мим.

Попробуйте

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

Теперь я показывал% PDF -1.7 7 0 obj < >/Annots [5 0 R]/PZ 1 >> endobj 8 0 obj и т. Д. – HeartDisk

+0

Где это показано? На странице? В консоли? – stormdrain

+0

в консоли, я пробовал почти все – HeartDisk

0

Try This

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

И передать необходимые переменные в ГЭТ.

Это будет выглядеть нормально, но отлично работает.

+0

та же ошибка :( – HeartDisk

+0

Я использовал библиотеку загрузки CI для скачивания файла в вызываемой функции. будет очень легко использовать, вы должны передать имя файла для загрузки pdf и путь к pdf 'code'force_download ('Req File Name.pdf', 'Path to pdf'),' code' – Rajasekhar

Смежные вопросы